pthread_create
#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
Compile and link with -pthread.
编译时应注意。
#include<iostream>
#include<string>
#include<vector>
#include<pthread.h>
#include<unistd.h>using namespace std;void *start_routine(void *args)
{string name=static_cast<char*>(args);while(true){cout<<"new thread create success , name: "<<name<<endl;sleep(1);}
}int main()
{// vector<pthread_t> tids;
#define NUM 10for(int i=0;i<NUM;i++){pthread_t tid;char namebuffer[64];snprintf(namebuffer,sizeof(namebuffer),"%s:%d","thread",i);//pthread_create pthread_create(&tid,nullptr,start_routine,namebuffer);// pthread_create(&tid,nullptr,start_routine,(void*)"thread one");sleep(1);}while(true){cout<<"new thread create success,name : main thread"<<endl;sleep(1);}return 0;
}
//可重入函数->void *start_routine(void *args)
//局部变量具有临时性,在多线程中也适用--->每个线程都有自己独立的栈结构
cnt: 9 &cnt 0x7fa036708ef4
cnt: 9 &cnt 0x7fa035f07ef4
cnt: 9 &cnt 0x7fa035706ef4
cnt: 9 &cnt 0x7fa034f05ef4
cnt: 9 &cnt 0x7fa034704ef4
cnt: 9 &cnt 0x7fa033f03ef4
cnt: 9 &cnt 0x7fa033702ef4
cnt: 9 &cnt 0x7fa032f01ef4
cnt: 9 &cnt 0x7fa032700ef4
cnt: 9 &cnt 0x7fa031effef4
void *start_routine(void *args)
{sleep(1);ThreadData *td=static_cast<ThreadData*>(args);int cnt=10;while(cnt){cout<<"cnt: "<<cnt<<" &cnt "<<&cnt<<endl;cnt--;// cout<<"new thread create success , name: "<<td->namebuffer<<" cnt: "<<cnt--<<endl;sleep(1);} delete td;return nullptr;
}
#include<iostream>
#include<string>
#include<vector>
#include<pthread.h>
#include<unistd.h>using namespace std;
class ThreadData
{
public:pthread_t tid;char namebuffer[64];
};
//可重入函数
//局部变量具有临时性,在多线程中也适用--->每个线程都有自己独立的栈结构
void *start_routine(void *args)
{sleep(1);ThreadData *td=static_cast<ThreadData*>(args);int cnt=10;while(cnt){cout<<"cnt: "<<cnt<<" &cnt "<<&cnt<<endl;cnt--;// cout<<"new thread create success , name: "<<td->namebuffer<<" cnt: "<<cnt--<<endl;sleep(1);} delete td;return nullptr;
}int main()
{vector<ThreadData*> threads; #define NUM 10for(int i=0;i<NUM;i++){ThreadData *td=new ThreadData(); // char namebuffer[64];snprintf(td->namebuffer,sizeof(td->namebuffer),"%s:%d","thread",i+1);//pthread_create pthread_create(&td->tid,nullptr,start_routine,td);// pthread_create(&tid,nullptr,start_routine,(void*)"thread one");// sleep(1);threads.push_back(td);}for(auto &iter:threads){cout<<"create thread: "<<iter -> namebuffer << ":"<<iter->tid<<" success"<<endl;}while(true){cout<<"new thread create success,name : main thread"<<endl;sleep(1); }return 0;
}
pthread_exit
#include <pthread.h>
void pthread_exit(void *retval);
Compile and link with -pthread.
//可重入函数
//局部变量具有临时性,在多线程中也适用--->每个线程都有自己独立的栈结构
void *start_routine(void *args)
{sleep(1);ThreadData *td=static_cast<ThreadData*>(args);int cnt=10;while(cnt){cout<<"cnt: "<<cnt<<" &cnt "<<&cnt<<endl;cnt--;// cout<<"new thread create success , name: "<<td->namebuffer<<" cnt: "<<cnt--<<endl;sleep(1);pthread_exit(nullptr);//-> return nullptr// //任何一个执行流调exit整个进程都会退出// exit(0);//不能用来终止线程} delete td;//return nullptr;//线程函数结束,return的时候线程就算终止了
}
pthread_exit(nullptr);//-> return nullptr
线程等待:pthread_join
#include <pthread.h>
int pthread_join(pthread_t thread, void **retval);
Compile and link with -pthread.
线程异常,收到信号,整个进程都会退出。
线程取消: pthread_cancel
#include <pthread.h>
int pthread_cancel(pthread_t thread);
int pthread_cancel(pthread_t thread);
Compile and link with -pthread.
线程如果是被取消的,退出码: -1(PTHREAD_CANCELED);