23,125
社区成员
发帖
与我相关
我的任务
分享
#include <pthread.h>
#include <iostream>
#include <unistd.h>
#include <cstdio>
using namespace std;
void *thread(void *arg)
{
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);
cout<<"in thread, tid = "<<pthread_self()<<endl;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
pause();
return NULL;
}
int main()
{
pthread_t tid;
if(pthread_create(&tid, NULL, thread, 0)==-1)
{
perror("create");
return 1;
}
if(pthread_cancel(tid)==-1)
{
perror("cancel");
return 2;
}
void *r;
if(pthread_join(tid, (void**)&r)==-1)
{
perror("join");
return 3;
}
cout<< PTHREAD_CANCELED<<endl;
cout<<r<<endl;
cout<<"in main thread, tid = "<<pthread_self()<<endl;
return 0;
}
#include <iostream>
#include <unistd.h>
#include <cstdio>
using namespace std;
void *thread(void *arg)
{
cout<<"in thread, tid = "<<pthread_self()<<endl;
pause();
return NULL;
}
int main()
{
pthread_t tid;
if(pthread_create(&tid, NULL, thread, 0)==-1)
{
perror("create");
return 1;
}
sleep(1);
if(pthread_cancel(tid)==-1)
{
perror("cancel");
}
void *r;
if(pthread_join(tid, (void**)&r)==-1)
{
perror("join");
}
cout<< PTHREAD_CANCELED<<endl;
cout<<r<<endl;
cout<<"in main thread, tid = "<<pthread_self()<<endl;
return 0;
}