FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
thread/example/monitor.cpp at develop · boostorg/thread · GitHub
boostorg
/
thread
Public
Notifications
You must be signed in to change notification settings
Fork
171
Star
213
Code
Issues
77
Pull requests
13
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
thread
/
example
/
monitor.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
113 lines (101 loc) · 2.38 KB
Breadcrumbs
thread
/
example
/
monitor.cpp
Copy path
File metadata and controls
113 lines (101 loc) · 2.38 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//
Copyright (C) 2001-2003
//
William E. Kempf
//
//
Distributed under the Boost Software License, Version 1.0. (See accompanying
//
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#
include
<
vector
>
#
include
<
iostream
>
#
include
<
boost/thread/condition_variable.hpp
>
#
include
<
boost/thread/mutex.hpp
>
#
include
<
boost/thread/recursive_mutex.hpp
>
#
include
<
boost/thread/thread_only.hpp
>
namespace
{
const
int
ITERS
=
100
;
boost::mutex io_mutex;
}
//
namespace
template
<
typename
M>
class
buffer_t
{
public:
typedef
boost::unique_lock<M> scoped_lock;
buffer_t
(
int
n)
: p(
0
), c(
0
), full(
0
), buf(n)
{
}
void
send
(
int
m)
{
scoped_lock
lk
(mutex);
while
(full == buf.
size
())
cond.
wait
(lk);
buf[p] = m;
p = (p+
1
) % buf.
size
();
++full;
cond.
notify_one
();
}
int
receive
()
{
scoped_lock
lk
(mutex);
while
(full ==
0
)
cond.
wait
(lk);
int
i = buf[c];
c = (c+
1
) % buf.
size
();
--full;
cond.
notify_one
();
return
i;
}
static
buffer_t
&
get_buffer
()
{
static
buffer_t
buf
(
2
);
return
buf;
}
static
void
do_sender_thread
()
{
for
(
int
n =
0
; n <
ITERS
; ++n)
{
{
boost::unique_lock<boost::mutex>
lock
(io_mutex);
std::cout <<
"
sending:
"
<< n << std::endl;
}
get_buffer
().
send
(n);
}
}
static
void
do_receiver_thread
()
{
for
(
int
x=
0
; x < (
ITERS
/
2
); ++x)
{
int
n =
get_buffer
().
receive
();
{
boost::unique_lock<boost::mutex>
lock
(io_mutex);
std::cout <<
"
received:
"
<< n << std::endl;
}
}
}
private:
M mutex;
boost::condition_variable_any cond;
unsigned
int
p, c, full;
std::vector<
int
> buf;
};
template
<
typename
M>
void
do_test
(M* dummy=
0
)
{
(
void
)dummy;
typedef
buffer_t
<M> buffer_type;
buffer_type::get_buffer
();
boost::thread
thrd1
(&buffer_type::do_receiver_thread);
boost::thread
thrd2
(&buffer_type::do_receiver_thread);
boost::thread
thrd3
(&buffer_type::do_sender_thread);
thrd1.
join
();
thrd2.
join
();
thrd3.
join
();
}
void
test_buffer
()
{
do_test<boost::mutex>();
do_test<boost::recursive_mutex>();
}
int
main
()
{
test_buffer
();
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL