N***m 发帖数: 4460 | 1 I am a green hand in c++ and boost::thread. I have the following question.
Any comment is appreciated:)
Simply put, I want to manipulate a file with two threads:
one for read and one for write. When the total access number exceeds a given
limit, they just return. End of thread and main.
So in theory,read/write should be in equal probability. But I found
their behaviors tend to cluster. So the question is that: is my
observation wrong? or My understanding about mutex etc is wrong?
or anything els |
z****e 发帖数: 2024 | 2 你这个lock了两个不同的mutex啊。你这情况下,等于没用一样。 |
N***m 发帖数: 4460 | 3 3x a lot! I made a stupid mistake again:)
I need to learn it carefully before programming.
【在 z****e 的大作中提到】 : 你这个lock了两个不同的mutex啊。你这情况下,等于没用一样。
|
N***m 发帖数: 4460 | 4 大虾,能不能再指点一下。
我刚改成了
namespace
{
int num=0;//given access limit
boost::mutex mutex_;
}
去掉readtext/writetext里面的local mutex_,
这样是不是就是一个mutex?
但是运行出来的还是差不多。
【在 z****e 的大作中提到】 : 你这个lock了两个不同的mutex啊。你这情况下,等于没用一样。
|
N***m 发帖数: 4460 | 5 I know what's going on le.
【在 N***m 的大作中提到】 : 大虾,能不能再指点一下。 : 我刚改成了 : namespace : { : int num=0;//given access limit : boost::mutex mutex_; : } : 去掉readtext/writetext里面的local mutex_, : 这样是不是就是一个mutex? : 但是运行出来的还是差不多。
|
z****e 发帖数: 2024 | 6 mutex的作用是要保护一个 critical section。就是一段公用的代码,
你的两个线程,完全不会去执行一段相同的代码。
故而,mutex也就失去了作用。
【在 N***m 的大作中提到】 : 大虾,能不能再指点一下。 : 我刚改成了 : namespace : { : int num=0;//given access limit : boost::mutex mutex_; : } : 去掉readtext/writetext里面的local mutex_, : 这样是不是就是一个mutex? : 但是运行出来的还是差不多。
|
X****r 发帖数: 3557 | 7 你说的是critical section,和mutex不完全是一回事,
虽然可以用mutex来实现critical section。
【在 z****e 的大作中提到】 : mutex的作用是要保护一个 critical section。就是一段公用的代码, : 你的两个线程,完全不会去执行一段相同的代码。 : 故而,mutex也就失去了作用。
|
z****e 发帖数: 2024 | 8 能不能给展开讲讲?
mutex 和 critical section的联系和区别?
【在 X****r 的大作中提到】 : 你说的是critical section,和mutex不完全是一回事, : 虽然可以用mutex来实现critical section。
|
X****r 发帖数: 3557 | 9 ……你自己找本书看看吧。
【在 z****e 的大作中提到】 : 能不能给展开讲讲? : mutex 和 critical section的联系和区别?
|
z****e 发帖数: 2024 | 10 什么书?
【在 X****r 的大作中提到】 : ……你自己找本书看看吧。
|