Chinaunix

标题: 【求助】主线程如何杀死正在执行的子线程? [打印本页]

作者: dongpy    时间: 2006-08-23 12:00
标题: 【求助】主线程如何杀死正在执行的子线程?
示例代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

void* func(void *)
{
while (1)  
{
;
}
return NULL;
}

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

pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

if ( pthread_create(&thrd, &attr, func, NULL) )
{
perror("pthread_create error");
exit(EXIT_FAILURE);
}

if ( !pthread_cancel(thrd) )
{
printf( "pthread_cancel OK\n" );
}

sleep( 10 );
return 0;
}

//pthread_cancel执行成功了,但是子线程仍然在内存里运行,请问这是为什么呢?

P.S.是这样的一个要求,在主线程里创建一个执行线程,执行一些操作,这些操作有可能是死循环,这时主线程就要负责杀死这个执行线程,请问能不能实现呢?
作者: cjaizss    时间: 2006-08-23 12:07

  1. void* func(void *)
  2. {
  3. pthread_detach(pthread_self());
  4. while (1)  
  5. {
  6. ;
  7. }
  8. return NULL;
  9. }
复制代码

作者: dongpy    时间: 2006-08-23 14:20
试了下楼上的方法,不行!
作者: keanlee    时间: 2006-08-23 15:08
在pthread_create与pthread_cancel之间添加sleep(1)。
因为调度的问题,确保让子线程先执行。
作者: foolfoolbird    时间: 2006-08-23 15:14
也要请教此类问题
关注中
作者: nnnqpnnn    时间: 2006-08-23 16:22
pthread-kill();
然后看你的信号处理了
作者: 醉卧水云间    时间: 2006-08-23 18:55
我写了个multiget,大把的cancel。
google multiget去找吧。
作者: dongpy    时间: 2006-08-24 11:42
试了很多方法,都不成功,郁闷。。。

我怀疑是不是要子线程挂起后,才能被杀掉?

带有死循环的子线程是不会被挂起的,这种情况不知道该如何杀掉?
作者: dongpy    时间: 2006-08-24 12:00
能不能在主线程中将正在运行的子线程挂起呢?
作者: dongpy    时间: 2006-08-25 21:16
Up~
作者: benlan    时间: 2006-08-25 21:22
system("
killall pid
or
kill -9 pid
")
作者: redac    时间: 2007-03-02 09:00
使用楼上的方法的话整个进程都被杀掉了!!!
作者: caijimin    时间: 2007-03-02 09:36
看看下面这段,是否有帮助。

Never terminate the thread from outside the thread. Always terminate the thread from within the thread itself. If the thread isn't allowed to terminate itself, it will continue to consume resources even after the app is closed. Resources being memory for stack space and thread local storage, objects that are created by the thread, file buffers, etc.

Your thread will typically be running in a loop. If it's not, it will terminate automatically when it hits a return statement in the thread function or at the end of the thread function.
While looping in the thread function there are a couple of things you can do.

1. Periodically check the status of a flag variable that indicates to exit the thread (The flag can be set externally to the thread itself), and exit the loop once the flag is set.

2. Periodically check for an event to have occurred using one of the wait functions.
作者: jemy.zhang    时间: 2007-03-02 12:46
我是这样做的,
fork进程以后,记住子进程的进程号pid,然后kill(pid,9);就可以了。

或者通过管道向子进程发送自杀信号,子进程在循环中检测信号,一旦检测到主进程要求子进程“自杀”,那么子进程就可以exit了


  1. //子进程,检测信号
  2.        while(1)
  3.         {
  4.             pipe_read(CMDPIPE,pipe_info);//读取管道
  5.             if(pipe_info->sig_term == 1) //
  6.             {
  7.                 free(pipe_info);
  8.                 kill(pid,9);//同时结束另外一个子进程
  9.                  return(1);//自身退出
  10.             }
  11.             sleep(1);
  12.         }
复制代码

[ 本帖最后由 jemy.zhang 于 2007-3-2 12:55 编辑 ]
作者: caijimin    时间: 2007-03-02 13:39
原帖由 jemy.zhang 于 2007-3-2 12:46 发表
我是这样做的,
fork进程以后,记住子进程的进程号pid,然后kill(pid,9);就可以了。

或者通过管道向子进程发送自杀信号,子进程在循环中检测信号,一旦检测到主进程要求子进程“自杀”,那么子进程就可以exit了 ...

楼主是说要杀子线程吧, 不是子进程
作者: jemy.zhang    时间: 2007-03-02 13:50
哦。。。。看错了,呼呼呼~

不过有谁能说说进程和线程的区别啊?忘记了似乎
作者: 柳五随风    时间: 2007-03-03 01:09
原帖由 caijimin 于 2007-3-2 09:36 发表
看看下面这段,是否有帮助。

Never terminate the thread from outside the thread. Always terminate the thread from within the thread itself. If the thread isn't allowed to terminate itself, it will ...



这个已经说得相当的明白了。
作者: hubei_1218    时间: 2007-03-13 14:55
楼主在线程函数死循环里面加上cancellation函数例如pthread_testcancel,因为线程创建出来初始状态是PTHREAD_CANCEL_DEFERRED的,也就是线程需要运行到cancellation point才会响应其他线程的cancel request,不知道说明白没有.




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2