Skip to content

Commit a1ff370

Browse files
committed
mylib-blockingqueue: sync in method 'peek'
1 parent 7190195 commit a1ff370

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

cpp/cpp-boost/mylib-blockingqueue.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class BlockingQueue {
102102

103103
// returns false if queue is empty, otherwise returns true and assigns the result
104104
bool peek(T& result) const {
105-
uniquelk lk(mut);
105+
uniquelk(mut);
106106

107107
if (q.empty()) {
108108
return false;
@@ -114,7 +114,7 @@ class BlockingQueue {
114114

115115

116116
void clear() {
117-
uniquelk lk(mut);
117+
uniquelk(mut);
118118

119119
while (false == q.empty()) {
120120
q.pop();

cpp/cpp-pthread/mylib-blockingqueue.hpp

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -83,38 +83,38 @@ class BlockingQueue {
8383

8484
// sync enqueue
8585
void put(const T& value) {
86-
int ret = 0;
86+
int tmp = 0;
8787

88-
ret = pthread_mutex_lock(&mut);
88+
tmp = pthread_mutex_lock(&mut);
8989

9090
while (q.size() >= capacity) {
91-
ret = pthread_cond_wait(&condFull, &mut);
91+
tmp = pthread_cond_wait(&condFull, &mut);
9292
}
9393

9494
q.push(value);
9595

96-
ret = pthread_mutex_unlock(&mut);
97-
ret = pthread_cond_signal(&condEmpty);
96+
tmp = pthread_mutex_unlock(&mut);
97+
tmp = pthread_cond_signal(&condEmpty);
9898
}
9999

100100

101101
// sync dequeue
102102
T take() {
103103
T result;
104-
int ret = 0;
104+
int tmp = 0;
105105

106-
ret = pthread_mutex_lock(&mut);
106+
tmp = pthread_mutex_lock(&mut);
107107

108108
while (q.empty()) {
109109
// Queue is empty, must wait for 'put'
110-
ret = pthread_cond_wait(&condEmpty, &mut);
110+
tmp = pthread_cond_wait(&condEmpty, &mut);
111111
}
112112

113113
result = q.front();
114114
q.pop();
115115

116-
ret = pthread_mutex_unlock(&mut);
117-
ret = pthread_cond_signal(&condFull);
116+
tmp = pthread_mutex_unlock(&mut);
117+
tmp = pthread_cond_signal(&condFull);
118118

119119
return result;
120120
}
@@ -125,28 +125,35 @@ class BlockingQueue {
125125
// Note: For asynchronous operations, we should use a long-live background thread
126126
// instead of using a temporary thread
127127
pthread_t tid;
128+
int tmp;
128129
auto arg = new PendingData(this, value);
129-
pthread_create(&tid, nullptr, &BlockingQueue<T>::putPending, arg);
130-
pthread_detach(tid);
130+
tmp = pthread_create(&tid, nullptr, &BlockingQueue<T>::putPending, arg);
131+
tmp = pthread_detach(tid);
131132
}
132133

133134

134135
// returns false if queue is empty, otherwise returns true and assigns the result
135136
bool peek(T& result) const {
136-
if (q.empty()) {
137-
return false;
137+
bool ret = false;
138+
139+
int tmp;
140+
tmp = pthread_mutex_lock(&mut);
141+
142+
if (false == q.empty()) {
143+
result = q.front();
144+
ret = true;
138145
}
139146

140-
result = q.front();
141-
return true;
147+
tmp = pthread_mutex_unlock(&mut);
148+
return ret;
142149
}
143150

144151

145152
void clear() {
146-
int ret;
147-
ret = pthread_mutex_lock(&mut);
153+
int tmp;
154+
tmp = pthread_mutex_lock(&mut);
148155
std::queue<T>().swap(q);
149-
ret = pthread_mutex_unlock(&mut);
156+
tmp = pthread_mutex_unlock(&mut);
150157
}
151158

152159

cpp/cpp-std/mylib-blockingqueue.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class BlockingQueue {
102102

103103
// returns false if queue is empty, otherwise returns true and assigns the result
104104
bool peek(T& result) const {
105-
// uniquelk lk(mut);
105+
uniquelk(mut);
106106
if (q.empty()) {
107107
return false;
108108
}
@@ -113,7 +113,7 @@ class BlockingQueue {
113113

114114

115115
void clear() {
116-
uniquelk lk(mut);
116+
uniquelk(mut);
117117
std::queue<T>().swap(q);
118118
}
119119

0 commit comments

Comments
 (0)