您的位置:程序门 -> c/c++ -> c++ 语言



如何暂停正在运行的四个线程?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何暂停正在运行的四个线程?
发表于:2007-09-27 09:34:09 楼主
如何暂停正在运行的四个线程?
a   =   afxbeginthread(threadfunc1,this);    
b   =   afxbeginthread(threadfunc2,this);  
c   =   afxbeginthread(threadfunc3,this);
d   =   afxbeginthread(threadfunc4,this);
发表于:2007-09-27 09:38:031楼 得分:0
开始的时候暂停还是执行中暂停?
发表于:2007-09-27 10:09:492楼 得分:0
dword   suspendthread(   handle   hthread);
发表于:2007-09-27 10:31:413楼 得分:0
执行中暂停?
发表于:2007-09-27 10:32:004楼 得分:0
执行中暂停
发表于:2007-09-27 10:34:135楼 得分:0
3楼的就可以。开始的时候暂停有个参数可以设置。
发表于:2007-09-27 11:54:236楼 得分:0
很讨厌,mfc提供了包裹createthread的宏,却找不到有包裹suspendthread的宏
发表于:2007-09-27 13:47:327楼 得分:0
res1   =   suspendthread(a);
res2   =   suspendthread(b);
res3   =   suspendthread(c);
res4   =   suspendthread(d);
执行后没有起到作用!
应该如何解决啊  
我是打算用一个stop   button控制的,希望是按一下暂停,再按一下继续.不知道怎么办才好.
void   xxx::onbnclickedbuttonstop()
发表于:2007-09-27 22:19:138楼 得分:0
不要使用suspendthread!!!

1.   容易造成死锁。例如:线程a运行new操作符分配内存,多线程环境的new操作是要加锁的,如果正在已经加锁但还没有解锁的情况下线程a被suspend了,那么进程内的其它线程只要一调用new操作符则必死无疑,死锁!!
msdn中对suspendthread的remark:
this   function   is   primarily   designed   for   use   by   debuggers.   it   is   not   intended   to   be   used   for   thread   synchronization.   calling   suspendthread   on   a   thread   that   owns   a   synchronization   object,   such   as   a   mutex   or   critical   section,   can   lead   to   a   deadlock   if   the   calling   thread   tries   to   obtain   a   synchronization   object   owned   by   a   suspended   thread.   to   avoid   this   situation,   a   thread   within   an   application   that   is   not   a   debugger   should   signal   the   other   thread   to   suspend   itself.   the   target   thread   must   be   designed   to   watch   for   this   signal   and   respond   appropriately.

2.   suspendthread之类的api都是针对user   mode的,当线程运行在kernel   mode时,这些操作都是不起作用的。例如线程a正在调用waitforsingleobject函数等待某个内核对象时,调用suspendthread(a)是不起作用的。
发表于:2007-09-28 10:58:309楼 得分:0
直接用一个标志变量m_bpaused
发表于:2007-09-28 11:19:0410楼 得分:0
study
发表于:2007-09-29 01:30:1311楼 得分:0
暂停线程ms不是好的设计方法
不过api也有提供这样的功能,可以参考楼上说的用一全局标志变量,来决定线程是否可以退出


快速检索

最新资讯
热门点击