|
|
|
|
 线程取消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)]
|
|
|
|
[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]
|
|
[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]
|
|
|