免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7364 | 回复: 7
打印 上一主题 下一主题

如何在主线程中让一个子线程退出? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-06-09 21:14 |只看该作者 |倒序浏览
使用了use threads;多线程模式,在实际的编码中,有一个让子线程退出的操作,我在主线程中使用$thr->kill('KILL')->detach();这种模式让指定的子线程退出,但在子线程退出后,主线程会也会退出,并报Signal SIGKILL received, but no signal handler set.,但是我希望主线程不要退出,该怎么做呢?多谢

论坛徽章:
46
15-16赛季CBA联赛之四川
日期:2018-03-27 11:59:132015年亚洲杯之沙特阿拉伯
日期:2015-04-11 17:31:45天蝎座
日期:2015-03-25 16:56:49双鱼座
日期:2015-03-25 16:56:30摩羯座
日期:2015-03-25 16:56:09巳蛇
日期:2015-03-25 16:55:30卯兔
日期:2015-03-25 16:54:29子鼠
日期:2015-03-25 16:53:59申猴
日期:2015-03-25 16:53:29寅虎
日期:2015-03-25 16:52:29羊年新春福章
日期:2015-03-25 16:51:212015亚冠之布里斯班狮吼
日期:2015-07-13 10:44:56
2 [报告]
发表于 2011-06-09 22:34 |只看该作者
threads 的文档说的很清楚啊

EXITING A THREAD

The usual method for terminating a thread is to return EXPR from the entry point function with the appropriate return value(s).

    * threads->exit()

      If needed, a thread can be exited at any time by calling threads->exit() . This will cause the thread to return undef in a scalar context, or the empty list in a list context.

      When called from the main thread, this behaves the same as exit(0).
    * threads->exit(status)

      When called from a thread, this behaves like threads->exit() (i.e., the exit status code is ignored).

      When called from the main thread, this behaves the same as exit(status).
    * die()

      Calling die() in a thread indicates an abnormal exit for the thread. Any $SIG{__DIE__} handler in the thread will be called first, and then the thread will exit with a warning message that will contain any arguments passed in the die() call.
    * exit(status)

      Calling exit EXPR inside a thread causes the whole application to terminate. Because of this, the use of exit() inside threaded code, or in modules that might be used in threaded applications, is strongly discouraged.

      If exit() really is needed, then consider using the following:

         1. threads->exit() if threads->can('exit'); # Thread friendly
         2. exit(status);

    * use threads 'exit' => 'threads_only'

      This globally overrides the default behavior of calling exit() inside a thread, and effectively causes such calls to behave the same as threads->exit() . In other words, with this setting, calling exit() causes only the thread to terminate.

      Because of its global effect, this setting should not be used inside modules or the like.

      The main thread is unaffected by this setting.
    * threads->create({'exit' => 'thread_only'}, ...)

      This overrides the default behavior of exit() inside the newly created thread only.
    * $thr->set_thread_exit_only(boolean)

      This can be used to change the exit thread only behavior for a thread after it has been created. With a true argument, exit() will cause only the thread to exit. With a false argument, exit() will terminate the application.

      The main thread is unaffected by this call.
    * threads->set_thread_exit_only(boolean)

      Class method for use inside a thread to change its own behavior for exit().

      The main thread is unaffected by this call.

论坛徽章:
0
3 [报告]
发表于 2011-06-10 09:55 |只看该作者
我是想在主线程中让子线程退出,而不是在子线程中自行退出,上述有函数符合这个要求吗?

论坛徽章:
46
15-16赛季CBA联赛之四川
日期:2018-03-27 11:59:132015年亚洲杯之沙特阿拉伯
日期:2015-04-11 17:31:45天蝎座
日期:2015-03-25 16:56:49双鱼座
日期:2015-03-25 16:56:30摩羯座
日期:2015-03-25 16:56:09巳蛇
日期:2015-03-25 16:55:30卯兔
日期:2015-03-25 16:54:29子鼠
日期:2015-03-25 16:53:59申猴
日期:2015-03-25 16:53:29寅虎
日期:2015-03-25 16:52:29羊年新春福章
日期:2015-03-25 16:51:212015亚冠之布里斯班狮吼
日期:2015-07-13 10:44:56
4 [报告]
发表于 2011-06-10 10:10 |只看该作者
我是想在主线程中让子线程退出,而不是在子线程中自行退出,上述有函数符合这个要求吗?
sclouder 发表于 2011-06-10 09:55



    抱歉是我没看清楚,你设个 handler 不就行了?把 SIGUSR1 设成退出当前线程

论坛徽章:
0
5 [报告]
发表于 2011-06-10 10:17 |只看该作者
好,我来试一下

论坛徽章:
46
15-16赛季CBA联赛之四川
日期:2018-03-27 11:59:132015年亚洲杯之沙特阿拉伯
日期:2015-04-11 17:31:45天蝎座
日期:2015-03-25 16:56:49双鱼座
日期:2015-03-25 16:56:30摩羯座
日期:2015-03-25 16:56:09巳蛇
日期:2015-03-25 16:55:30卯兔
日期:2015-03-25 16:54:29子鼠
日期:2015-03-25 16:53:59申猴
日期:2015-03-25 16:53:29寅虎
日期:2015-03-25 16:52:29羊年新春福章
日期:2015-03-25 16:51:212015亚冠之布里斯班狮吼
日期:2015-07-13 10:44:56
6 [报告]
发表于 2011-06-10 10:18 |只看该作者
回复 5# sclouder


    文档中有个很清晰的示例
   1. use threads;
   2.
   3. sub thr_func
   4. {
   5. # Thread 'cancellation' signal handler
   6. local $SIG{'KILL'} = sub { threads->exit(); };
   7.
   8. ...
   9. }
  10.
  11. # Create a thread
  12. my $thr = threads->create('thr_func');
  13.
  14. ...
  15.
  16. # Signal the thread to terminate, and then detach
  17. # it so that it will get cleaned up automatically
  18. $thr->kill('KILL')->detach();

论坛徽章:
0
7 [报告]
发表于 2011-06-10 10:37 |只看该作者
真的非常感谢对这个问题的细致回答,你的方法我试验成功了,不过在使用threads中还碰到一个问题,子线程退出后一段时间会报错:
Use of uninitialized value in null operation during global destruction.
请问这个错误一般是怎么引起的?

论坛徽章:
46
15-16赛季CBA联赛之四川
日期:2018-03-27 11:59:132015年亚洲杯之沙特阿拉伯
日期:2015-04-11 17:31:45天蝎座
日期:2015-03-25 16:56:49双鱼座
日期:2015-03-25 16:56:30摩羯座
日期:2015-03-25 16:56:09巳蛇
日期:2015-03-25 16:55:30卯兔
日期:2015-03-25 16:54:29子鼠
日期:2015-03-25 16:53:59申猴
日期:2015-03-25 16:53:29寅虎
日期:2015-03-25 16:52:29羊年新春福章
日期:2015-03-25 16:51:212015亚冠之布里斯班狮吼
日期:2015-07-13 10:44:56
8 [报告]
发表于 2011-06-10 11:23 |只看该作者
真的非常感谢对这个问题的细致回答,你的方法我试验成功了,不过在使用threads中还碰到一个问题,子线程退出 ...
sclouder 发表于 2011-06-10 10:37



    线程结束的时候需要释放内存,释放的时候出错了。这方面经验不多,不过看高手们的意思这个问题应该无解了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP