#include "stdafx.h"
#include "ace/Synch.h"
#ifdef _DEBUG
#pragma comment(lib,"aced")
#else
#pragma comment(lib,"aced")
#endif
static int number=0;
static int seed=0;
#include "ace/Thread_Manager.h"
//static const int DEFAULT_THREADS = ACE_DEFAULT_THREADS;
//static const int DEFAULT_ITERATIONS = 100000;
static void *worker (int iterations)
{
ACE_OS::printf("ddd\n");
ACE_OS::sleep (1);
for (int i = 0; i < iterations; i++)
{
//ACE_DEBUG ((LM_DEBUG,"(%t) checking cancellation before iteration %d!\n", i));
//Before doing work check if you have been canceled. If so don’t
//do any more work.
if (ACE_Thread_Manager::instance ()->testcancel(ACE_Thread::self ()) != 0)
{//Worker函数中的每个线程将使用ACE_Thread_Manager的testcancel()来检查取消情况
//ACE_DEBUG ((LM_DEBUG,"(%t) has been canceled before iteration %d!\n",i));
break;
}
ACE_OS::printf("循环次数\n");
}
return 0;
}
int main (int argc, char *argv[])
{
int n_threads = 1;
int n_iterations = 10;
ACE_Thread_Manager *thr_mgr = ACE_Thread_Manager::instance ();
//Create a group of threads n_threads that will execute the worker
//function the spawn_n method returns the group ID for the group of
//threads that are spawned. The argument n_iterations is passed back
//to the worker. Notice that all threads are created detached.
int grp_id = thr_mgr->spawn_n (n_threads, ACE_THR_FUNC (worker),(void *) n_iterations,THR_NEW_LWP | THR_DETACHED);
// Wait for 1 second and then suspend every thread in the group.
ACE_OS::sleep (1);
//ACE_DEBUG ((LM_DEBUG, "(%t) suspending group\n"));
if (thr_mgr->suspend_grp (grp_id) == -1)//挂起
{
ACE_ERROR ((LM_DEBUG, "(%t) %p\n", "Could not suspend_grp"));
}
// Wait for 1 more second and then resume every thread in the
// group.
ACE_OS::sleep (5);
//ACE_DEBUG ((LM_DEBUG, "(%t) resuming group\n"));
if (thr_mgr->resume_grp (grp_id) == -1)//恢复
{
ACE_ERROR ((LM_DEBUG, "(%t) %p\n", "resume_grp"));
}
// Wait for 1 more second and then cancel all the threads.
//ACE_DEBUG ((LM_DEBUG, "(%t) canceling group\n"));
if (thr_mgr->cancel_grp (grp_id) == -1)//取消
{
ACE_ERROR ((LM_DEBUG, "(%t) %p\n", "cancel_grp"));
}
// Perform a barrier wait until all the threads have shut down.
thr_mgr->wait ();
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ACE线程代码实例.rar (11个子文件)
线程同步栅栏.txt 1KB
条件等待.txt 2KB
ACE_TSS模板的线程专有变量.txt 2KB
使用令牌互斥方法.txt 2KB
主动对象创建任务.txt 1KB
主动对象任务间通信.txt 2KB
ACE_Thread_Manage类线程挂起、继续、取消操作.txt 2KB
创建线程和线程互斥.txt 1KB
守卫锁也是自动锁方法.txt 2KB
ACE_Thread_Manager类创建线程组.txt 2KB
动态更换互斥方法.txt 2KB
共 11 条
- 1
资源评论
- xionggw2014-09-25基本都是从网上摘抄的一些代码和文章,实用性不高,初学者可以看看。
- awxfen2015-01-13初学者有用,项目需要的可用性不高,自己整理的网上代码
liforgetbain
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功