没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示


试读
25页
这是陈硕在 2009 年上海 C++ 技术大会演讲《当析构函数遇到多线程》的 PPT 投影片,可自由用于个人学习,其他使用需得到作者许可。 简介:编写线程安全的类不是难事,用同步原语保护内部状态即可。但是对象的生与死不能由对象自身拥有的互斥器来保护。如何保证即将析构对象 x 的时候,不会有另一个线程正在调用 x 的成员函数?或者说,如何保证在执行 x 的成员函数期间,对象 x 不会在另一个线程被析构?如何避免这种 race condition 是 C++ 多线程编程面临的基本问题,可以借助 tr1 中的 shared_ptr 和 weak_ptr 完美解决。这也是实现线程安全的 Observer 模式的必备技术。
资源推荐
资源详情
资源评论










blog
.
csdn
.
net
/
Solstice
giantchen
@
gmail
.
com
当析构函数遇到多线程
多线程中 C++ 对象的生与死
这是陈硕在 2009 年上海 C++ 技术大会演讲的投影片,可自由用
于个人学习,其他使用需得到作者许可。
编写线程安全的类不是难事,用同步原语保护内部状态即可。但是
对象的生与死不能由对象自身拥有的互斥器来保护。如何保证即将析构
对象 x 的时候,不会有另一个线程正在调用 x 的成员函数?或者说,如
何保证在执行 x 的成员函数期间,对象 x 不会在另一个线程被析构?如
何避免这种 race condition 是 C++ 多线程编程面临的基本问题,可以
借助 tr1 中的 shared_ptr 和 weak_ptr 完美解决。这也是实现线程安全
的 Observer 模式的必备技术。
在此基础上,还将介绍一个线程安全的、contention-free 的
Signals/Slots 设计与实现,以及借助 shared_ptr 实现线程安全的
copy-on-write。
blog
.
csdn
.
net
/
Solstice
giantchen
@
gmail
.
com
2
When destructors come
across multi-threading
Birth and death of objects in threads
2009-12
陈硕
Shuo Chen
blog.csdn.net/Solstice
giantchen@gmail.com

blog
.
csdn
.
net
/
Solstice
giantchen
@
gmail
.
com
3
Intended audience
•
C++ programmers aware of
–
tr1::shared_ptr, tr1::weak_ptr
–
tr1::function, tr1::bind
–
deadlocks and race conditions
–
Mutex and MutexLock
blog
.
csdn
.
net
/
Solstice
giantchen
@
gmail
.
com
4
Agenda
• Part I 45 min
–
Object lifetime management in multi-threads
• Mini QA
5 min
•
Part II 30 min
–
Thread safe signals/slots
• QA 10 min

blog
.
csdn
.
net
/
Solstice
giantchen
@
gmail
.
com
5
Object lifetime in multi-threads
• In C++, programmers manage lifetime of
objects by themselves
• Things become much more complicated
when multi-threading comes in to play
•
When you are about to delete an object, how
do you know that it is not being used in
another thread?
• How can you tell if the object is still alive
before you trying call its member function?
blog
.
csdn
.
net
/
Solstice
giantchen
@
gmail
.
com
6
Mutex and MutexLock
• Mutex wraps creation and destruction of
–
A CRITICAL_SECTION on Windows
–
A pthread_mutex_t on Linux
•
MutexLock wraps acquiring and releasing
–
enter or leave critical section on Windows
–
lock or unlock pthreads mutex on Linux
• Both are not copy-constructible or assignable

blog
.
csdn
.
net
/
Solstice
giantchen
@
gmail
.
com
7
Thread safety
•
A class is thread-safe
–
if it behaves correctly when accessed from
multiple threads
–
regardless of the scheduling or interleaving of
the execution of those threads by the OS
–
and with no additional synchronization or other
coordination on the part of the calling code
Java Concurrency in Practice, by Brian Goetz et al.
blog
.
csdn
.
net
/
Solstice
giantchen
@
gmail
.
com
8
A thread safe Counter class
•
Write a thread safe class is not difficult
–
Protect internal state via synchronization
•
How about its birth and death?
class Counter : noncopyable
{
public:
Counter(): value_(0) {}
int64_t value() const;
int64_t increase();
int64_t decrease();
private:
int64_t value_;
mutable Mutex mutex_;
}
int64_t Counter::value() const
{
MutexLock lock(mutex_);
return value_;
}
int64_t Counter::increase()
{
MutexLock lock(mutex_);
int64_t ret = value_++;
return ret;
}
* In real world, atmoic operations are better for Counter.

blog
.
csdn
.
net
/
Solstice
giantchen
@
gmail
.
com
9
Bear easily
• Do not allow this pointer to escape during
construction
–
Don't register to any callback in ctor (*)
–
Even at last line of ctor is also bad
// Don't do this.
class Foo : public Observer
{
public:
Foo(Observable* s) {
s->register(this); // X
}
virtual void update();
};
// Do this.
class Foo : public Observer
{ // ...
void observe(Observable* s) {
s->register(this);
}
};
Foo* pFoo = new Foo;
Observable* s = getIt();
pFoo->observe(s);
(*) unless you know it will not call you back in any other thread
blog
.
csdn
.
net
/
Solstice
giantchen
@
gmail
.
com
10
Die hard
•
You can't tell if a object is alive by looking at
the pointer or reference
–
if it's dead, an invalid object won't tell anything
–
set the pointer to NULL after delete? helpless
•
There must be some alive object help us
telling other object's state
• But, pointer and reference are not objects,
they are primitive types
剩余24页未读,继续阅读

陈硕
- 粉丝: 1w+
- 资源: 3
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


安全验证
文档复制为VIP权益,开通VIP直接复制

- 1
- 2
- 3
- 4
- 5
- 6
前往页