URN Logo
UNIX Resources » Linux » China Linux Forum » C/C++编程版 » 63 » 线程取消pthread_cancel的问题
announcement 声明: 本页内容为中国Linux论坛的内容镜像,文章的版权以及其他所有的相关权利属于中国Linux论坛和相应文章的作者,如果转载,请注明文章来源及相关版权信息。
Resources
China Linux Forum(finished)
Linux Forum(finished)
FreeBSD China(finished)
linuxforum.net
  业界新闻与评论
  自由软件杂谈
  IT 人生
  Linux软件快递
  翻译作坊
  Linux图书与评论
  GNU Emacs/XEmacs
  Linux 中文环境和中文化
  Linux桌面与办公软件
  Linux 多媒体与娱乐版
  自由之窗Mozilla
  笔记本电脑上的Linux
  Gentoo
  Debian 一族
  网络管理技术
  Linux 安装与入门
  WEB服务器和FTP服务器
  域名服务器和邮件服务器
  Linux防火墙和代理服务器应用
  文件及打印服务器
  技术培训与认证
  Linux内核技术
  Linux 嵌入技术
  Linux设备驱动程序
  Linux 集群技术
  LINUX平台数据库
  系统和网络安全
  CPU 与 编译器
  系统计算研究所专栏
  Linux下的GUI软件开发
  C/C++编程版
  PHP 技 术
  Java&jsp技术
  Shell编程技术
  Perl 编 程
  Python 编 程
  XML/Web Service 技术
  永远的Unix
  FreeBSD世界
   
线程取消pthread_cancel的问题
线程取消pthread_cancel的问题 - BillGates [2004-01-29 10:36 | 4,551 byte(s)]
 
Re: 线程取消pthread_cancel的问题 - dl_dht [2004-01-29 13:49 | 2,755 byte(s)]
 
Re: 线程取消pthread_cancel的问题 - BillGates [2004-01-29 17:54 | 77 byte(s)]
 
Subject: 线程取消pthread_cancel的问题
Author: BillGates    Posted: 2004-01-29 10:36    Length: 4,551 byte(s)
[Original] [Print] [Top]
各位,下面这段程序在RH7.3, 8.0都能取得满意结果,但在RH9.0(内核2.4.20-8)上面却不能取得
任何结果,本意是主程序可以控制取消子线程,按照Posix的说法,sem_wait()是可以作为取消点
的,但为何在RH9.0上却不能奏效呢?

注:pthread_setcancelstate()和pthread_testcancel()在RH7.3及8.0均可不加。
可以用ps mx | grep thread_cancel 命令(RH8.0以上)来查看线程运行及个数

---------------------------------------------------------------------------------------------------------------------------------

/*
* thread_cancel.cpp
*
* this program is to test the pthread_cancel in multi-threads
*
*/

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <pthread.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <signal.h>
#include <semaphore.h>

pthread_t thd1 ;
pthread_t thd2 ;
pthread_t thd3 ;
pthread_t thd4 ;

sem_t sem1 ;

int my_thread_create(pthread_t *thread, void * (*proc)(void *), void *arg) {

if (pthread_create(thread, NULL, proc, arg)!=0) {
printf("pthread_create() failed ");
return -1;
}

return 0;
}

int my_thread_detach_create(pthread_t *thread, void * (*proc)(void *), void *arg) {

if (pthread_create(thread, NULL, proc, arg)!=0) {
printf("pthread_create() failed ");
return -1;
}

if (pthread_detach(*thread)!=0) {
printf("pthread_detach() failed ");
return -1;
}

return 0;
}


void * thd1_proc(void * arg)
{
int i = 0 ;
double j = 0 ;

printf("i come to thread 1 time ") ;

pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) ;

pthread_testcancel();
sem_wait(&sem1) ;

pthread_testcancel();

printf("after sem_wait() in thread 1 ") ;


while(i < 100000)
{
j = 102.388 * 476834.2 ;
i++ ;
}

//return NULL ;
pthread_exit(NULL);
}


int main(int argc, char * argv[])
{
int i = 0 ;
double j = 0 ;


if (sem_init(&sem1, 0, 0) != 0)
{
printf("init sem 1 error ") ;
exit(1) ;
}


my_thread_create(&thd1, thd1_proc, NULL) ;
my_thread_create(&thd2, thd1_proc, NULL) ;
my_thread_create(&thd3, thd1_proc, NULL) ;
my_thread_create(&thd4, thd1_proc, NULL) ;

while(1)
{
//pause() ;

printf("before thread cancel ") ;
sleep(10) ;

if (pthread_cancel(thd1) !=0)
printf("cancel thread 1 error ") ;

printf("after thread cancel 1 ") ;
sleep(5) ;

if (pthread_cancel(thd2) !=0)
printf("cancel thread 2 error ") ;

printf("after thread cancel 2 ") ;
sleep(5) ;

if (pthread_cancel(thd3) != 0)
printf("cancel thread 3 error ") ;

printf("after thread cancel 3 ") ;
sleep(5) ;

if (pthread_cancel(thd4) !=0)
printf("cancel thread 4 error ") ;

printf("after thread cancel 4 ") ;

break;
}


while(1)
pause() ;

return 0 ;
}


----
爱拼才会赢......
[Original] [Print] [Top]
Subject: Re: 线程取消pthread_cancel的问题
Author: dl_dht    Posted: 2004-01-29 13:49    Length: 2,755 byte(s)
[Original] [Print] [Top]
I have test your program on rh9.0 . the result is same like what you have said . I modified your code to use pthread_cleanup_push, pthread_cleanup_pop as follows.

void cleanup_handler (void *arg)
{
printf("sem wait over %d ", pthread_self());
}

void *thd1_proc(void *arg)
{
int i = 0;
double j = 0;

pthread_cleanup_push(cleanup_handler, NULL);
printf("i come to thread 1 time ");

pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_testcancel();
sem_wait(&sem1);
pthread_cleanup_pop(1);
........................
}

on RH9.0 (2.6.1)
[root@dht c]# ./ex01
i come to thread 1 time
i come to thread 1 time
i come to thread 1 time
i come to thread 1 time
before thread cancel
after thread cancel 1
after thread cancel 2
after thread cancel 3
after thread cancel 4

on RH 7.1 (2.4.7-10)
[root@eight tmp]# ./ex01
i come to thread 1 time
i come to thread 1 time
i come to thread 1 time
before thread cancel
i come to thread 1 time
after thread cancel 1
sem wait over 1026
after thread cancel 2
sem wait over 2051
after thread cancel 3
sem wait over 3076
after thread cancel 4
sem wait over 4101


then I add this :

void *thd1_proc(void *arg)
{
int i = 0;
double j = 0;

pthread_cleanup_push(cleanup_handler, NULL);
printf("i come to thread 1 time ");

pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
pthread_testcancel();
sem_wait(&sem1);
pthread_cleanup_pop(1);
........................
}

on RH9.0 (2.6.1)
[root@dht c]# ./ex01
i come to thread 1 time
i come to thread 1 time
i come to thread 1 time
i come to thread 1 time
before thread cancel
after thread cancel 1
sem wait over 1082346688
after thread cancel 2
sem wait over 1090739264
after thread cancel 3
sem wait over 1099127744
after thread cancel 4
sem wait over 1116941120


It's OK !
[Original] [Print] [Top]
Subject: Re: 线程取消pthread_cancel的问题
Author: BillGates    Posted: 2004-01-29 17:54    Length: 77 byte(s)
[Original] [Print] [Top]

Good! It is very kind of you !

thanks.
----
爱拼才会赢......
[Original] [Print] [Top]
« Previous thread
MYSQL自带的mysqlclient的库函数为什么崩溃
C/C++编程版
63
Next thread »
关于串口编程
     

Copyright © 2007 UNIX Resources Network, All Rights Reserved.      About URN | Privacy & Legal | Help | Contact us
备案序号: 京ICP备05006143    webmaster: webmaster@unixresources.net
This page created on 2008-07-17 03:54:16, cost 0.071832895278931 ms.