由买买提看人间百态

topics

全部话题 - 话题: lpbuf
(共0页)
k*******t
发帖数: 202
1
来自主题: JobHunting版 - 请教一段volatile和多线程的代码
class SyncBuf {
public:
void Thread1();
void Thread2();
private:
typedef vector BufT;
volatile BufT buffer_;
Mutex mtx_; // controls access to buffer_
};
Inside a thread function, you simply use a LockingPtr to get
controlled access to the buffer_ member variable:
void SyncBuf::Thread1() {
LockingPtr lpBuf(buffer_, mtx_);
BufT::iterator i = lpBuf->begin();
for (; i != lpBuf->end(); ++i) {
... use *i ...
}
}
The code is very easy to wr... 阅读全帖
(共0页)