S**Y 发帖数: 136 | 1 请写一个thread-safe的class,用c++,
讲思路
我说的是写一个monitor那样的class,来保证一些methods synchronized
不知道对不对...c++又不直接提供像java那样的monitor. |
q***q 发帖数: 3356 | 2 用一个static变量做mutex?
【在 S**Y 的大作中提到】 : 请写一个thread-safe的class,用c++, : 讲思路 : 我说的是写一个monitor那样的class,来保证一些methods synchronized : 不知道对不对...c++又不直接提供像java那样的monitor.
|
I*****y 发帖数: 602 | 3 主要是对所有的成员变量存取都要做lock/unlock。
1. 可以声明所有成员变量为protected or private, 然后专门写存取函数。
2. 内部函数用到的地方也需要做控制,如果允许的话,就用1的存取函数存取。如果性
能优先的话,内部函数用的地方加unlock/lock。
【在 S**Y 的大作中提到】 : 请写一个thread-safe的class,用c++, : 讲思路 : 我说的是写一个monitor那样的class,来保证一些methods synchronized : 不知道对不对...c++又不直接提供像java那样的monitor.
|
t****u 发帖数: 8614 | 4 1) Define a mutex.
2) for each public/protected member function, put mutex lock at beginning,
and unlock at the end.
【在 S**Y 的大作中提到】 : 请写一个thread-safe的class,用c++, : 讲思路 : 我说的是写一个monitor那样的class,来保证一些methods synchronized : 不知道对不对...c++又不直接提供像java那样的monitor.
|
P********e 发帖数: 2610 | 5 mutex is slow
use criticalsection
【在 t****u 的大作中提到】 : 1) Define a mutex. : 2) for each public/protected member function, put mutex lock at beginning, : and unlock at the end.
|
t****t 发帖数: 6806 | 6 critical section is windows only, not portable.
【在 P********e 的大作中提到】 : mutex is slow : use criticalsection
|
P********e 发帖数: 2610 | 7 or there must be a portable kernel level something in other OS:)
【在 t****t 的大作中提到】 : critical section is windows only, not portable.
|
r****e 发帖数: 3 | 8 you can take a look at boost thread library. It may have what you want. |
t****t 发帖数: 6806 | 9 on the contrary, pthread use mutex for critical section.
【在 P********e 的大作中提到】 : or there must be a portable kernel level something in other OS:)
|
p***o 发帖数: 1252 | 10 pthread has a spinlock. Probably you can use that to construct
something similar to critical section in Win32.
【在 t****t 的大作中提到】 : on the contrary, pthread use mutex for critical section.
|
t****t 发帖数: 6806 | 11 true, but i heard it's not support everywhere either?
【在 p***o 的大作中提到】 : pthread has a spinlock. Probably you can use that to construct : something similar to critical section in Win32.
|
p***o 发帖数: 1252 | 12 I don't know. They are supposed to be useful for multi-core systems.
Anyway, those coarse grained locks are not good in terms of multi-core
performance.
【在 t****t 的大作中提到】 : true, but i heard it's not support everywhere either?
|