|
|
发表于 2005-3-29 13:15:20
|
显示全部楼层
Post by icoming
我的那个程序与下面的模拟程序差不多,不知道为什么cancel的时候老出错
#include <iostream>
using namespace std;
#include <pthread.h>
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
void *thread_run(void *)
{
cout<<"------------thread start"<<endl;
//pthread_cond_signal(&cond);
int state;
while(true){
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE , &state);
cout<<"-----------thread loop"<<endl;
for(int i=0 ; i < 10000 ; i++);
cout<<"----------start to sleep"<<endl;
pthread_setcancelstate(state , &state);
sleep(1);
}
}
int main()
{
for(int i=0 ; i < 1000 ; i++){
pthread_t id;
cout<<"create a thread"<<endl;
pthread_create(&id , NULL , thread_run , NULL);
//cout<<"wait for the new thread to run"<<end;
//pthread_cond_wait(&cond , &mutex);
cout<<"cancel a thread"<<endl;
pthread_cancel(id);
void *reval;
cout<<"wait for a thread to die"<<endl;
pthread_join(id , &reval);
cout<<endl<<endl;
}
}
线程可能还没创建完,你就试图去退出线程,当然有问题了。改为这样吧
sleep(1);
//pthread_cond_wait(&cond , &mutex);
我也不太懂的,说错别怪 |
|