-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
91 lines (74 loc) · 2.08 KB
/
test.cpp
File metadata and controls
91 lines (74 loc) · 2.08 KB
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
#include <iostream>
#include <string>
#include <cstdlib>
#include <boost/thread/mutex.hpp>
#include "thread_adaptor.hpp"
// user defined
struct thread_adaptor_data
{
std::size_t count_;
std::string buf_;
};
// thread_process1
THREAD_ADAPTOR_PROCESS_BEGIN(thread_process1, data)
{
std::cout << '[' << data.buf_ << ']' << std::endl;
if (data.count_ < 26)
{
THREAD_ADAPTOR_JOIN(thread_adaptor_data, thread_process1)
{
THREAD_ADAPTOR_DATA_MEMBER(count_) = data.count_;
THREAD_ADAPTOR_DATA_MEMBER(buf_) = data.buf_;
THREAD_ADAPTOR_DATA_MEMBER(buf_) +=
'a' + THREAD_ADAPTOR_DATA_MEMBER(count_)++;
}
}
}
THREAD_ADAPTOR_PROCESS_END
// thread_process2
THREAD_ADAPTOR_PROCESS_BEGIN(thread_process2, data)
{
std::cout << '[' << data.buf_ << ']' << std::endl;
if (data.count_ < 10)
{
data.buf_ += '0' + data.count_++;
THREAD_ADAPTOR_JOIN_WITH_DATAOBJ(thread_process2, data);
}
}
THREAD_ADAPTOR_PROCESS_END
// thread_process3
boost::mutex process3_mutex;
THREAD_ADAPTOR_PROCESS_BEGIN(thread_process3, data)
{
for (; data.count_ < data.buf_.size(); ++data.count_)
{
process3_mutex.lock();
std::cout << data.count_ << ": " << data.buf_[data.count_] << '\n';
process3_mutex.unlock();
}
}
THREAD_ADAPTOR_PROCESS_END
int main(void)
{
THREAD_ADAPTOR_JOIN(thread_adaptor_data, thread_process1)
{
THREAD_ADAPTOR_DATA_MEMBER(count_) = 0;
THREAD_ADAPTOR_DATA_MEMBER(buf_) = "";
}
thread_adaptor_data data;
data.count_ = 0;
data.buf_ = "";
THREAD_ADAPTOR_JOIN_WITH_DATAOBJ(thread_process2, data);
std::cout << std::endl;
THREAD_ADAPTOR_DETACH(thread_adaptor_data, thread_process3)
{
THREAD_ADAPTOR_DATA_MEMBER(count_) = 0;
THREAD_ADAPTOR_DATA_MEMBER(buf_) = "hello world!";
}
data.count_ = 0;
data.buf_ = "HELLO WORLD!";
THREAD_ADAPTOR_DETACH_WITH_DATAOBJ(thread_process3, data);
usleep(1 * 1000 * 1000);
std::cout << "success main." << std::endl;
return EXIT_SUCCESS;
}