peakzhang 发表于 2007-12-24 23:11:29

帮帮忙ACE_Condition问题


ACE_Thread_Mutex g_mutex;
ACE_Condition<ACE_Thread_Mutex> g_cond(g_mutex);

int g_requestNum = 1;

class CMyTask : public ACE_Task<ACE_MT_SYNCH>
{
public:

CMyTask()
{

}

int svc()
{
ACE_DEBUG((LM_DEBUG,"Thread <%t> enter time: %T\n"));

if (g_requestNum < 1)
{
   g_cond.wait();   
   }

--g_requestNum;

ACE_DEBUG((LM_DEBUG,"Thread <%t> exit time: %T\n"));
return 0;
}

int open(void)
{
ACE_DEBUG((LM_DEBUG,"Thread <%t> open!\n"));
this->activate(THR_NEW_LWP | THR_JOINABLE,3);
return 0;
}

int close(u_long flags)
{
ACE_DEBUG((LM_DEBUG,"Thread <%t> close!\n"));
return 0;
}


protected:
private:

};


int main(int argc, char* argv[])
{

CMyTask oCMyTask;
oCMyTask.open();


int sw = 0;
std::cin<<sw;
if (sw == 1)
{
++g_requestNum;
::g_cond.signal();   //执行后等待的线程没有继续执行,不知道哪里错了。
}

return 0;
}

peakzhang 发表于 2007-12-24 23:11:55


#include "ace/Task.h"
#include "ace/Synch.h"
#include "ace/IOStream.h"

ACE_Thread_Mutex g_mutex;
ACE_Condition<ACE_Thread_Mutex> g_cond(g_mutex);

int g_requestNum = 1;

class CMyTask: public ACE_Task<ACE_MT_SYNCH>
{
public:
CMyTask(){}

int svc()
{
ACE_DEBUG((LM_DEBUG, "Thread <%t> enter time: %T\n"));
if( g_requestNum == 1 ){
   g_cond.wait();
}
-- g_requestNum;
ACE_DEBUG((LM_DEBUG, "Thread <%t> exit time: %T\n"));
return 0;
}

int open(void)
{
ACE_DEBUG((LM_DEBUG, "Thread <%t> open!\n"));
this->activate(THR_NEW_LWP| THR_JOINABLE, 3);
return 0;
}

int close(u_long flags)
{
ACE_DEBUG((LM_DEBUG, "Thread <%t> close!\n"));
return 0;
}
protected:
private:
};


int main(int argc, char* argv[])
{
CMyTask oCMyTask;
oCMyTask.open();

int sw = 1;

for( int i = 0 ; i < 3 ; i++)
{
++ g_requestNum;
::g_cond.signal();
}

oCMyTask.wait();
return 0;
}


蛮好的了,不知道你是指什么意思

peakzhang 发表于 2007-12-24 23:12:31

这几天调试代码发现有点错误

#include<iostream>
#include <ace/Synch.h>
#include<ace/Task.h>
using namespace std;

ACE_Thread_Mutex g_mutex;
ACE_Condition<ACE_Thread_Mutex> g_cond(g_mutex);
int g_requestNum = 1;   //只有一个资源可以使用
class CMyTask : public ACE_Task<ACE_MT_SYNCH>
{
public:
CMyTask()
{
}
int svc()
{
ACE_DEBUG((LM_DEBUG,"Thread <%t> enter time: %T\n"));
if (g_requestNum < 1)
{
   g_cond.wait();   //资源数不足,将线程等待
   }
--g_requestNum; //资源使用完后,将资源数置减1
ACE_DEBUG((LM_DEBUG,"Thread <%t> exit time: %T\n"));
return 0;
}
int open(void)
{
ACE_DEBUG((LM_DEBUG,"Thread <%t> open!\n"));
this->activate(THR_NEW_LWP | THR_JOINABLE,3);//定义3个线程
return 0;
}
int close(u_long flags)
{
ACE_DEBUG((LM_DEBUG,"Thread <%t> close!\n"));
return 0;
}

protected:
private:
};

int main(int argc, char* argv[])
{
CMyTask oCMyTask;
oCMyTask.open();
int sw = 0;
while (true)
{
cin>>sw;
if (sw == 1)
{
   ++g_requestNum;//输入1后增加一个资源
   ::g_cond.signal();//资源增加后唤醒等待的线程,但是问题就是等待的线程没有启动,
                     //不知道是怎么回事?请大家帮着看看!
}
}

return 0;
}

http://localhost/bbs/Skins/Default/emot/em04.gifhttp://localhost/bbs/Skins/Default/emot/em04.gifhttp://localhost/bbs/Skins/Default/emot/em04.gifhttp://localhost/bbs/Skins/Default/emot/em04.gif

peakzhang 发表于 2007-12-24 23:12:44

if (g_requestNum < 1)   这里一开始 g_requestNum = 1,怎么等待?
{
   g_cond.wait();   //资源数不足,将线程等待
   }

peakzhang 发表于 2007-12-24 23:13:25

到今天我已经知道了原因,特发表:
ACE_Thread_Mutex g_mutex;
ACE_Condition<ACE_Thread_Mutex> g_cond(g_mutex);
int g_requestNum = 1;
class CMyTask : public ACE_Task<ACE_MT_SYNCH>
{
public:
CMyTask()
{
}
int svc()
{
ACE_DEBUG((LM_DEBUG,"Thread <%t> enter time: %T\n"));
g_mutex.aquire();
if (g_requestNum < 1)
{
   g_cond.wait();   
   }
--g_requestNum;
g_mutex.release();
ACE_DEBUG((LM_DEBUG,"Thread <%t> exit time: %T\n"));
return 0;
}
int open(void)
{
ACE_DEBUG((LM_DEBUG,"Thread <%t> open!\n"));
this->activate(THR_NEW_LWP | THR_JOINABLE,3);
return 0;
}
int close(u_long flags)
{
ACE_DEBUG((LM_DEBUG,"Thread <%t> close!\n"));
return 0;
}

protected:
private:
};

int main(int argc, char* argv[])
{
CMyTask oCMyTask;
oCMyTask.open();

int sw = 0;
std::cin<<sw;
if (sw == 1)
{
++g_requestNum;
::g_cond.signal();
}
return 0;
}
页: [1]
查看完整版本: 帮帮忙ACE_Condition问题