-
Notifications
You must be signed in to change notification settings - Fork 0
/
PThreadsCond.cpp
162 lines (117 loc) · 2.97 KB
/
PThreadsCond.cpp
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <Windows.h>
#include <pthread.h>
#include <vector>
#include <queue>
/*
template<typename Data>
class concurrent_queue {
private:
std::queue<Data> the_queue;
mutable boost::mutex the_mutex;
boost::condition_variable the_condition_variable;
public:
void push(Data const& data) {
boost::mutex::scoped_lock lock(the_mutex);
the_queue.push(data);
lock.unlock();
the_condition_variable.notify_one();
}
bool empty() const {
boost::mutex::scoped_lock lock(the_mutex);
return the_queue.empty();
}
bool try_pop(Data& popped_value) {
boost::mutex::scoped_lock lock(the_mutex);
if (the_queue.empty()) {
return false;
}
popped_value=the_queue.front();
the_queue.pop();
return true;
}
void wait_and_pop(Data& popped_value) {
boost::mutex::scoped_lock lock(the_mutex);
while (the_queue.empty()) {
the_condition_variable.wait(lock);
}
popped_value=the_queue.front();
the_queue.pop();
}
};
*/
bool IsPrime(size_t N) {
if (0 == (N % 2)) {
return false;
}
if (0 == (N % 3)) {
return false;
}
size_t K = 1;
size_t T = (6 * K);
while ( ((T - 1) * (T - 1)) <= N) {
if (0 == (N % (T - 1))) {
return false;
}
if (0 == (N % (T + 1))) {
return false;
}
++K;
T = (6 * K);
}
return true;
}
typedef struct TDataTAG {
std::queue<size_t> Queue_;
pthread_mutex_t Mutex_;
pthread_cond_t Cond_;
} TData;
void* ProducerThread(void* D) {
TData *Data = (TData*)D;
for (size_t i = 0; i < 100; ++i) {
pthread_mutex_lock(&Data->Mutex_);
for (size_t j = 0; j < 10; ++j) {
size_t t = (j + 1) * 1000 + i;
printf("Produciendo: %d Queue Size: %d\n", t, Data->Queue_.size());
Data->Queue_.push(t);
}
//pthread_cond_signal(&Data->Cond_);
pthread_cond_broadcast(&Data->Cond_);
pthread_mutex_unlock(&Data->Mutex_);
Sleep(3 * 1000);
}
return NULL;
}
void* ConsumerThread(void* D) {
pthread_t Self = pthread_self();
TData *Data = (TData*)D;
while (1) {
pthread_mutex_lock(&Data->Mutex_);
while (Data->Queue_.empty()) {
pthread_cond_wait(&Data->Cond_, &Data->Mutex_);
}
size_t N = Data->Queue_.front(); Data->Queue_.pop();
pthread_mutex_unlock(&Data->Mutex_);
printf("Procesando %p: %d\n", Self.p, N);
//IsPrime(N);
//Sleep(1 * 1000);
//Sleep(25);
}
return NULL;
}
int main(int argc, char *argv[]) {
TData Data;
pthread_mutex_init(&Data.Mutex_, NULL);
pthread_cond_init(&Data.Cond_, NULL);
std::vector<pthread_t> Consumers;
const size_t NumberOfConsumers = 2;
for (size_t N = 0; N < NumberOfConsumers; ++N) {
pthread_t PThread;
pthread_create(&PThread, NULL, ConsumerThread, (void*)&Data);
Consumers.push_back(PThread);
}
pthread_t Producer;
int Error = pthread_create(&Producer, NULL, ProducerThread, (void*)&Data);
for (size_t N = 0; N < NumberOfConsumers; ++N) {
pthread_join(Consumers[N], NULL);
}
}