由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 请教一段volatile和多线程的代码
相关主题
C++ Singleton的实现问一道无聊的bloomberg电面题
谁给改一个线程安全的smarter pointer类一个C语言概念题
C++ volatile请教搞不清C++里的constant expression
qualcomm 新鲜电面面经Regular expression matching 在什么输入下时间复杂度是O(2^n)?
MapR周五onsite,求blessfailed bloomberg phone interview
请问大牛们关于Regular expression matchingC++ Q83: 这个const_cast什么意思?
两道面试题,请大家说说看法google面试有问protocol buffer嘛
弱问个C++ 问题 (const_cast)攒人品。面试经历(1)
相关话题的讨论汇总
话题: buft话题: lockingptr话题: buffer话题: volatile话题: lpbuf
进入JobHunting版参与讨论
1 (共1页)
k*******t
发帖数: 202
1
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 write and understand — whenever you need to use
buffer_, you must create a LockingPtr pointing to it. Once you do that
, you have access to vector's entire interface.
The nice part is that if you make a mistake, the compiler will point it out:
void SyncBuf::Thread2() {
// Error! Cannot access 'begin' for a volatile object
BufT::iterator i = buffer_.begin();
// Error! Cannot access 'end' for a volatile object
for (; i != lpBuf->end(); ++i) {
... use *i ...
}
}
You cannot access any function of buffer_ until you either apply a const_
cast or use LockingPtr. The difference is that LockingPtr offers an ordered
way of applying const_cast to volatile variables.
LockingPtr is remarkably expressive. If you only need to call one function,
you can create an unnamed temporary LockingPtr object and use it directly:
unsigned int SyncBuf::Size() {
return LockingPtr(buffer_, mtx_)->size();
}
k*******t
发帖数: 202
2
谁能给解释一下啊?我没看明白的说。。。
k*******t
发帖数: 202
3
谁能给解释一下啊?我没看明白的说。。。
B********8
发帖数: 9
4
Are they from this page: http://drdobbs.com/cpp/184403766?
There are basically two points here:
1) LockPtr is a smartptr. It's ctor will call Mutex.aquire(); and its dtor
will call Mutex.release().
2) (This is harder) The smart usage of "volatile" and "const_cast<>" to make
user defined class variable/function thread-safe.
a) "volatile" object can NOT access normal members, such as "begin()", "
end()". It must be cast to "const" using const_cast<>
b) LockPtr will do this cast for you, when it overload "->" and "*"
operators.
Hope this help.
1 (共1页)
进入JobHunting版参与讨论
相关主题
攒人品。面试经历(1)MapR周五onsite,求bless
攒人品。面试经历(2)请问大牛们关于Regular expression matching
请问如果用C++实现Thread Safe Queue两道面试题,请大家说说看法
how to create thread-safe singleton?弱问个C++ 问题 (const_cast)
C++ Singleton的实现问一道无聊的bloomberg电面题
谁给改一个线程安全的smarter pointer类一个C语言概念题
C++ volatile请教搞不清C++里的constant expression
qualcomm 新鲜电面面经Regular expression matching 在什么输入下时间复杂度是O(2^n)?
相关话题的讨论汇总
话题: buft话题: lockingptr话题: buffer话题: volatile话题: lpbuf