找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 3827|回复: 4

帮帮忙ACE_Condition问题

[复制链接]
发表于 2007-12-24 23:11:29 | 显示全部楼层 |阅读模式
  1. ACE_Thread_Mutex g_mutex;
  2. ACE_Condition<ACE_Thread_Mutex> g_cond(g_mutex);
  3. int g_requestNum = 1;
  4. class CMyTask : public ACE_Task<ACE_MT_SYNCH>
  5. {
  6. public:
  7. CMyTask()
  8. {
  9. }
  10. int svc()
  11. {
  12.   ACE_DEBUG((LM_DEBUG,"Thread <%t> enter time: %T\n"));
  13.   if (g_requestNum < 1)
  14.   {
  15.    g_cond.wait();   
  16.    }
  17.   --g_requestNum;
  18.   ACE_DEBUG((LM_DEBUG,"Thread <%t> exit time: %T\n"));
  19.   return 0;
  20. }
  21. int open(void)
  22. {
  23.   ACE_DEBUG((LM_DEBUG,"Thread <%t> open!\n"));
  24.   this->activate(THR_NEW_LWP | THR_JOINABLE,3);
  25.   return 0;
  26. }
  27. int close(u_long flags)
  28. {
  29.   ACE_DEBUG((LM_DEBUG,"Thread <%t> close!\n"));
  30.   return 0;
  31. }
  32. protected:
  33. private:
  34. };
  35. int main(int argc, char* argv[])
  36. {
  37. CMyTask oCMyTask;
  38. oCMyTask.open();
  39. int sw = 0;
  40. std::cin<<sw;
  41. if (sw == 1)
  42. {
  43.   ++g_requestNum;
  44.   ::g_cond.signal();   //执行后等待的线程没有继续执行,不知道哪里错了。
  45. }
  46. return 0;
  47. }
复制代码
 楼主| 发表于 2007-12-24 23:11:55 | 显示全部楼层
  1. #include "ace/Task.h"
  2. #include "ace/Synch.h"
  3. #include "ace/IOStream.h"
  4. ACE_Thread_Mutex g_mutex;
  5. ACE_Condition<ACE_Thread_Mutex> g_cond(g_mutex);
  6. int g_requestNum = 1;
  7. class CMyTask: public ACE_Task<ACE_MT_SYNCH>
  8. {
  9. public:
  10. CMyTask(){}
  11. int svc()
  12. {
  13.   ACE_DEBUG((LM_DEBUG, "Thread <%t> enter time: %T\n"));
  14.   if( g_requestNum == 1 ){
  15.    g_cond.wait();
  16.   }
  17.   -- g_requestNum;
  18.   ACE_DEBUG((LM_DEBUG, "Thread <%t> exit time: %T\n"));
  19.   return 0;
  20. }
  21. int open(void)
  22. {
  23.   ACE_DEBUG((LM_DEBUG, "Thread <%t> open!\n"));
  24.   this->activate(THR_NEW_LWP| THR_JOINABLE, 3);
  25.   return 0;
  26. }
  27. int close(u_long flags)
  28. {
  29.   ACE_DEBUG((LM_DEBUG, "Thread <%t> close!\n"));
  30.   return 0;
  31. }
  32. protected:
  33. private:
  34. };
  35. int main(int argc, char* argv[])
  36. {
  37. CMyTask oCMyTask;
  38. oCMyTask.open();
  39. int sw = 1;
  40. for( int i = 0 ; i < 3 ; i++)
  41. {
  42.   ++ g_requestNum;
  43.   ::g_cond.signal();
  44. }
  45. oCMyTask.wait();
  46. return 0;
  47. }
复制代码
蛮好的了,不知道你是指什么意思
 楼主| 发表于 2007-12-24 23:12:31 | 显示全部楼层
这几天调试代码发现有点错误
  1. #include<iostream>
  2. #include <ace/Synch.h>
  3. #include<ace/Task.h>
  4. using namespace std;
  5. ACE_Thread_Mutex g_mutex;
  6. ACE_Condition<ACE_Thread_Mutex> g_cond(g_mutex);
  7. int g_requestNum = 1;   //只有一个资源可以使用
  8. class CMyTask : public ACE_Task<ACE_MT_SYNCH>
  9. {
  10. public:
  11. CMyTask()
  12. {
  13. }
  14. int svc()
  15. {
  16.   ACE_DEBUG((LM_DEBUG,"Thread <%t> enter time: %T\n"));
  17.   if (g_requestNum < 1)
  18.   {
  19.    g_cond.wait();   //资源数不足,将线程等待
  20.    }
  21.   --g_requestNum; //资源使用完后,将资源数置减1
  22.   ACE_DEBUG((LM_DEBUG,"Thread <%t> exit time: %T\n"));
  23.   return 0;
  24. }
  25. int open(void)
  26. {
  27.   ACE_DEBUG((LM_DEBUG,"Thread <%t> open!\n"));
  28.   this->activate(THR_NEW_LWP | THR_JOINABLE,3);  //定义3个线程
  29.   return 0;
  30. }
  31. int close(u_long flags)
  32. {
  33.   ACE_DEBUG((LM_DEBUG,"Thread <%t> close!\n"));
  34.   return 0;
  35. }
  36. protected:
  37. private:
  38. };
  39. int main(int argc, char* argv[])
  40. {
  41. CMyTask oCMyTask;
  42. oCMyTask.open();
  43. int sw = 0;
  44. while (true)
  45. {
  46.   cin>>sw;
  47.   if (sw == 1)
  48.   {
  49.    ++g_requestNum;  //输入1后增加一个资源
  50.    ::g_cond.signal();  //资源增加后唤醒等待的线程,但是问题就是等待的线程没有启动,
  51.                        //不知道是怎么回事?请大家帮着看看!
  52.   }
  53. }
  54. return 0;
  55. }
复制代码
 楼主| 发表于 2007-12-24 23:12:44 | 显示全部楼层
if (g_requestNum < 1)   这里一开始 g_requestNum = 1,怎么等待?
  {
   g_cond.wait();   //资源数不足,将线程等待
   }
 楼主| 发表于 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;
}
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-11-24 12:20 , Processed in 0.018901 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表