ACE_Thread_Mutex出问题了, 大家来看看
#include "ace/Task.h"#include "ace/INET_Addr.h"
#include "ace/SOCK_Stream.h"
#include "ace/SOCK_Connector.h"
#include "ace/Log_Msg.h"
#include "ace/Os.h"
#include "ace/Thread_Mutex.h"
using namespace std;
int count = 0;
class HA_Device_Repository
{
public:
HA_Device_Repository()
{
}
HA_Device_Repository(const HA_Device_Repository &other)
{
}
void update_device(int device_id, bool flag)
{
this->mutex_.acquire();
int thread_id = 0;
if (flag)
{
++count;
thread_id = 1;
for (int i=0; i<100000000; ++i)
{
}
}
else
{
--count;
thread_id = 2;
for (int i=0; i<1000000; ++i)
{
}
}
ACE_DEBUG ((LM_DEBUG, ACE_TEXT("(%t) updating device %d thread = %d\n"), device_id, thread_id));
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("count = %d\n"), count));
this->mutex_.release();
ACE_OS::sleep(1);
}
private:
ACE_Thread_Mutex mutex_;
};
class HA_Comm : public ACE_Task_Base
{
public:
enum {NUM_USES = 10};
HA_Comm(HA_Device_Repository &rep) : rep_(rep), flag(false)
{
}
virtual int svc(void)
{
ACE_DEBUG ((LM_DEBUG, ACE_TEXT("(%t) Handler Thread running\n")));
for (int i=0; i<NUM_USES; ++i)
{
this->rep_.update_device(i, flag);
}
return 0;
}
void setFlag(bool flag)
{
this->flag = flag;
}
bool getFlag()
{
return this->flag;
}
private:
HA_Device_Repository rep_;
bool flag;
};
int ACE_TMAIN(int argc, ACE_TCHAR *[])
{
ACE_DEBUG ((LM_DEBUG, ("(%t) main Thread running \n")));
HA_Device_Repository rep;
HA_Comm handler1(rep);
HA_Comm handler2(rep);
handler1.setFlag(true);
handler2.setFlag(false);
handler1.activate();
handler2.activate();
handler1.wait();
handler2.wait();
return 0;
}
输出结果:
(3680) main Thread running
(2428) Handler Thread running
(2156) Handler Thread running
(2156) updating device 0 thread = 2//下面打印count = -1才对, 怎么是0, 线程2先更新全局变量, 加了锁, 应该是-1啊, 只当 线程2释放锁, 线程1进入后, count++, 打印的count值为1才是。 好像在update_device(。。。)里面线程锁并没有起作用。 请问这是什么原因?count = 0
(2428) updating device 0 thread = 1
count = 0
(2156) updating device 1 thread = 2
count = -1
(2428) updating device 1 thread = 1
count = 0
(2156) updating device 2 thread = 2
count = -1
(2156) updating device 3 thread = 2
count = -1
(2428) updating device 2 thread = 1
count = -1
(2156) updating device 4 thread = 2
count = -2
(2428) updating device 3 thread = 1
count = -1
(2156) updating device 5 thread = 2
count = -2
(2428) updating device 4 thread = 1
count = -1
(2156) updating device 6 thread = 2
count = -2
(2156) updating device 7 thread = 2 //下面count 应该=-3才对, 但打印的是-2, 有点不理解
count = -2
(2428) updating device 5 thread = 1
count = -2
(2156) updating device 8 thread = 2
count = -3
(2428) updating device 6 thread = 1
count = -2
(2156) updating device 9 thread = 2
count = -3
(2428) updating device 7 thread = 1
count = -2
(2428) updating device 8 thread = 1
count = -1
(2428) updating device 9 thread = 1
count = 0
Press any key to continue
基于上面原因, 大牛们帮我分析一下, 原因在哪 这个问题怎么没人给我解释一下啊 我看了半天也没明白,看来得调试一下 这个问题是解决了, 把HA_Device_Repository类里面的私有变量:ACE_Thread_Mutex mutex_;定义为全局变量就OK了, 但不知道为什么我把定义为这个类的私有变量就不行, 弄不懂这是为什么 HA_Device_Repository rep;
不是只有一个实例,有多个实例存在。 老大一语道破玄机呀!
页:
[1]