Linux下C++多线程编程演示时遇到的问题
最近公司有关于linux程序的开发,作为新人的我马上去复习下linux,参考的是这篇博客
https://blog.csdn.net/zgege/article/details/79912418
上面教的是c操作线程,以为用c++没事,结果遇到了很多坑
pthread_cancel函数失效?
功能:取消一个执行中的线程
这是我稍微变过的代码
#include<pthread.h>
#include<unistd.h>
#include<iostream>
using namespace std;
void *runFun(void *data){
while(1){
cout<<"My name is"<<endl;
}
return NULL;
}
int main()
{
pthread_t tid;
void *exitCode;
char *name=static_cast<char*>("张三");I"
pthread_create(&tid,NULL,runFun,NULL);
sleep(1);
pthread_cancel(tid);
pthread_join(tid,&exitCode);
if(exitCode==PTHREAD_CANCELED){
cout<<"pthread_canceled"<<endl;