//为什么还会出现竞态条件
#define _REENTRANT
#include <ace/Singleton.h>
#include <ace/Thread_Manager.h>
#include <ace/Thread_Mutex.h>
#include <iostream>
using namespace std;
class normal_test
{
friend class ACE_Singleton<normal_test, ACE_Thread_Mutex>;
public:
void show_my_life();
int get_num(void){return num;}
int add_num(void){num++;}
private:
normal_test();
int num;
};
normal_test::normal_test(){}
typedef ACE_Singleton<normal_test, ACE_Thread_Mutex> NORMAL_TEST;
void normal_test::show_my_life()
{
cout << "i'm living at " << (void*)this << endl;
}
void * worker(void *)
{
for (int i = 0; i < 1000000; i++)
NORMAL_TEST::instance()->add_num();
}
int ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
NORMAL_TEST::instance()->show_my_life();
ACE_Thread_Manager m_thread_manager;
m_thread_manager.spawn_n(2, worker, 0);
m_thread_manager.wait();
cout << NORMAL_TEST::instance()->get_num() << endl;
return 0;
}
[ 本帖最后由 印随 于 2008-3-27 15:20 编辑 ] |