Redian新闻
>
Re: 中国人,你为什么不快乐?
avatar
Re: 中国人,你为什么不快乐?# Joke - 肚皮舞运动
k*t
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();
}
avatar
w*w
2
【 以下文字转载自 Seattle 讨论区 】
发信人: Stewie84 (Stewie84), 信区: Seattle
标 题: Re: 中国人,你为什么不快乐?
发信站: BBS 未名空间站 (Wed Feb 23 17:34:52 2011, 美东)
有些事情也是被夸大了。
我觉得我父母辈(和40岁以上的)的都很快乐,每家好几套房,资产都上百上千万;拿
死工资的每年十几,几十万,做生意的就更别说了。
主要是网上年轻人被房价害惨了。
但社会不是年轻人掌控的,中产阶级稳定我们天朝就会很稳定。
avatar
k*t
3
谁能给解释一下啊?我没看明白的说。。。
avatar
b*a
4
阶级仇恨就是这样产生的吧
avatar
k*t
5
谁能给解释一下啊?我没看明白的说。。。
avatar
g*n
6
明显该人亲戚朋友都应该被共产共妻

【在 b*****a 的大作中提到】
: 阶级仇恨就是这样产生的吧
avatar
B*8
7
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.
avatar
z*n
8
84年的,估计还在啃老呢。

【在 w***w 的大作中提到】
: 【 以下文字转载自 Seattle 讨论区 】
: 发信人: Stewie84 (Stewie84), 信区: Seattle
: 标 题: Re: 中国人,你为什么不快乐?
: 发信站: BBS 未名空间站 (Wed Feb 23 17:34:52 2011, 美东)
: 有些事情也是被夸大了。
: 我觉得我父母辈(和40岁以上的)的都很快乐,每家好几套房,资产都上百上千万;拿
: 死工资的每年十几,几十万,做生意的就更别说了。
: 主要是网上年轻人被房价害惨了。
: 但社会不是年轻人掌控的,中产阶级稳定我们天朝就会很稳定。

avatar
v*s
9
我家农村亲戚在乡下盖了好几套房,想怎么住就怎么住,没人非要去北京上海。
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。