LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1047|回复: 7

一个C语言中线程终止的问题

[复制链接]
发表于 2006-7-25 15:36:04 | 显示全部楼层 |阅读模式
主进程假设创建了thread1, thread2, thread3, 分别的执行函数为thread1(), thread2(), thread3().假设thread2想要终止thread1, 该怎么做啊。我尝试当thread2想要终止thread1的时候,thread2向thread1发送一个信号,thread1收到该信号后执行信号处理函数endsession(),在endsession中做pthread_exit(),我以为是可行的,但是好像不行,不知道pthread_exit是终止了哪个线程?我该如何来实现这个方案啊
 楼主| 发表于 2006-7-25 17:12:01 | 显示全部楼层
static int stopthread(void)
{
//      pthread_exit(NULL);
        pthread_cancel(stopid);
}
static int awhile(void)
{
        signal(47,stopthread);
        while(1);
}
int main(void)
{
        pthread_t  eventid,stopid;
        pthread_create(&eventid,NULL,(void*)genevent,NULL);
        pthread_create(&stopid,NULL,(void*)awhile,NULL);

        printf("the thread id in stopid= %ld\n",stopid);
        if(!pthread_join(stopid,NULL))
        {
                printf("the thread exit successfully!\n");
        }
}
其中genevent()函数会产生一个事件,然后发送一个信号47,我希望awhile()函数捕获到该信号之后,能够将stopid线程终止,真心请教
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-7-25 17:12:08 | 显示全部楼层
static int stopthread(void)
{
//      pthread_exit(NULL);
        pthread_cancel(stopid);
}
static int awhile(void)
{
        signal(47,stopthread);
        while(1);
}
int main(void)
{
        pthread_t  eventid,stopid;
        pthread_create(&eventid,NULL,(void*)genevent,NULL);
        pthread_create(&stopid,NULL,(void*)awhile,NULL);

        printf("the thread id in stopid= %ld\n",stopid);
        if(!pthread_join(stopid,NULL))
        {
                printf("the thread exit successfully!\n");
        }
}
其中genevent()函数会产生一个事件,然后发送一个信号47,我希望awhile()函数捕获到该信号之后,能够将stopid线程终止,真心请教
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-7-25 17:12:51 | 显示全部楼层
static int stopthread(void)
{
//      pthread_exit(NULL);
        pthread_cancel(stopid);
}
static int awhile(void)
{
        signal(47,stopthread);
        while(1);
}
int main(void)
{
        pthread_t  eventid,stopid;
        pthread_create(&eventid,NULL,(void*)genevent,NULL);
        pthread_create(&stopid,NULL,(void*)awhile,NULL);

        printf("the thread id in stopid= %ld\n",stopid);
        if(!pthread_join(stopid,NULL))
        {
                printf("the thread exit successfully!\n");
        }
}
其中genevent()函数会产生一个事件,然后发送一个信号47,我希望awhile()函数捕获到该信号之后,能够将stopid线程终止,请帮我看看有什么问题啊,在此谢了
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-7-26 10:04:32 | 显示全部楼层
没人帮我一把啊,xdjms救我
回复 支持 反对

使用道具 举报

发表于 2006-7-27 18:27:28 | 显示全部楼层
先看看pthread怎么用!!

signal信号在线程之间用有什么意义吗!?
回复 支持 反对

使用道具 举报

发表于 2006-7-31 20:46:39 | 显示全部楼层
创建线程,保存  id 为全局变量,各个线程使用id 控制 线程
回复 支持 反对

使用道具 举报

发表于 2006-7-31 22:19:42 | 显示全部楼层
Post by windwiny
创建线程,保存  id 为全局变量,各个线程使用id 控制 线程


这样也不可能实现A纯种通过纯种B的pthread_t来控制它啊!!

使用pthread_t只能控制线程自己.


其实可以用许多方法来实现啦,比如说条件变量,posix信号量,最简单的也就是变量了!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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