#include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg)
创建线程,并可以向创建的线程传递一个参数(见实例3,向线程传递总的线程个数,一个int变量);
#include <pthread.h> int pthread_join(pthread_t thread, void **retval)等待线程终止,并且可以获得线程的返回值;
#include <pthread.h> pthread_t pthread_self(void);获得线程自身的线程ID;
#include <pthread.h> void pthread_exit(void *retval);终止线程,并可以向主线程返回值;
#include <pthread.h> int pthread_detach(pthread_t thread);在创建的线程中调用该函数,使该线程脱离进程(这里的脱离并不是真正意义上的完全脱离;脱离之后,该线程仍然在该进程空间里运行,只不过是当该线程退出的时候,它的资源会自动释放,进程不用等待其并释放其资源;
总结:
1:如何创建一个或多个线程;
2:如何在主线程中等待刚才创建的线程;
3:如何获得线程的线程ID;
4:如何在向创建的线程传递信息;
5:如何在创建的线程中退出的时候向主线程传递信息;
6:如何使线程脱离主线程;
实例1:简单的创建两个线程,并在主线程中等待这两个线程,使用pthread_create, pthread_join, pthread_self
/************************************************************************* > File Name: create_join.cpp > Author: > Mail: > Created Time: 2015年12月16日 星期三 10时41分19秒 ************************************************************************/ #include<iostream> #include <unistd.h> #include <pthread.h> #include <cstdlib> using namespace std; void *task1(void *a) { cout << "This is threadA..." << "thread id is " << pthread_self() << endl; sleep(2); cout << "threadA exit.." << endl; } void *task2(void *b) { cout << "This is threadB..." << "thread id is " << pthread_self() << endl; sleep(2); cout << "ThreadB exit.." << endl; } int main() { int ret; pthread_t threadA, threadB; ret = pthread_create(&threadA, NULL, task1, NULL); if(ret != 0){ cout << "pthread_create error..." << endl; exit(-1); } ret = pthread_create(&threadB, NULL, task2, NULL); if(ret != 0){ cout << "pthread_create error..." << endl; exit(-1); } ret = pthread_join(threadA, NULL); if(ret != 0){ cout << "pthread_create error..." << endl; exit(-1); } ret = pthread_join(threadB, NULL); if(ret != 0){ cout << "pthread_create error..." << endl; exit(-1); } cout << "main thread exit..." << endl; exit(0); }
实例2:创建多个线程(个数自己指定),并在主线程中等待这些线程,使用pthread_create, pthread_join, pthread_self
/************************************************************************* > File Name: create_join.cpp > Author: > Mail: > Created Time: 2015年12月16日 星期三 10时41分19秒 ************************************************************************/ #include<iostream> #include <unistd.h> #include <pthread.h> #include <cstdlib> using namespace std; void *task(void *a) { cout << "This is thread..." << "thread id is " << pthread_self() << endl; sleep(2); cout << "thread exit.." << pthread_self() << endl; } int main(int argc, char *argv[]) { int ret; int i = 0; if(argc != 2){ cout << "You must input the number of thread you want to create..." << endl; exit(-1); } int thread_num = atoi(argv[1]); pthread_t threadId[thread_num]; for (i = 0; i < thread_num; ++i){ ret = pthread_create(&threadId[i], NULL, task, NULL); if(ret != 0){ cout << "pthread_create error..." << endl; exit(-1); } } for (i = 0; i < thread_num; ++i){ ret = pthread_join(threadId[i], NULL); if(ret != 0){ cout << "pthread_create error..." << endl; exit(-1); } } cout << "main thread exit..." << endl; exit(0); }
/************************************************************************* > File Name: create_join.cpp > Author: > Mail: > Created Time: 2015年12月16日 星期三 10时41分19秒 ************************************************************************/ #include<iostream> #include <unistd.h> #include <pthread.h> #include <cstdlib> using namespace std; void *task(void *a) { int i = 0; int * loop; loop = static_cast<int*> (a); for (i = 0; i < *loop; ++i){ cout << i+1 << ": This is thread..." << "thread id is " << pthread_self() << endl; sleep(1); } cout << "thread exit.." << pthread_self() << endl; } int main(int argc, char *argv[]) { int ret; int i = 0; if(argc != 2){ cout << "You must input the number of thread you want to create..." << endl; exit(-1); } int thread_num = atoi(argv[1]); pthread_t threadId[thread_num]; for (i = 0; i < thread_num; ++i){ ret = pthread_create(&threadId[i], NULL, task, (void *)&thread_num); if(ret != 0){ cout << "pthread_create error..." << endl; exit(-1); } } for (i = 0; i < thread_num; ++i){ ret = pthread_join(threadId[i], NULL); if(ret != 0){ cout << "pthread_create error..." << endl; exit(-1); } } cout << "main thread exit..." << endl; exit(0); }
实例4:创建多个线程(个数自己指定)同时向各个线程中传递一个整数值,并在主线程中等待这些线程,线程退出的时候向主线程返回值(将自身的线程id传给主线程);
使用pthread_create, pthread_join, pthread_self, pthread_exit
/************************************************************************* > File Name: create_join.cpp > Author: > Mail: > Created Time: 2015年12月16日 星期三 10时41分19秒 ************************************************************************/ #include<iostream> #include <unistd.h> #include <pthread.h> #include <cstdlib> using namespace std; void *task(void *a) { int i = 0; int * loop; pthread_t pid = pthread_self(); loop = static_cast<int*> (a); for (i = 0; i < *loop; ++i){ cout << i+1 << ": This is thread..." << "thread id is " << pid << endl; sleep(1); } cout << "thread exit.." << pid << endl; pthread_exit((void *)&pid); } int main(int argc, char *argv[]) { int ret; int i = 0; if(argc != 2){ cout << "You must input the number of thread you want to create..." << endl; exit(-1); } int thread_num = atoi(argv[1]); pthread_t threadId[thread_num]; for (i = 0; i < thread_num; ++i){ ret = pthread_create(&threadId[i], NULL, task, (void *)&thread_num); if(ret != 0){ cout << "pthread_create error..." << endl; exit(-1); } } pthread_t * pid[thread_num]; for (i = 0; i < thread_num; ++i){ ret = pthread_join(threadId[i], (void **)&pid[i]); if(ret != 0){ cout << "pthread_create error..." << endl; exit(-1); } } for (i = 0; i < thread_num; ++i){ cout << "main thread wait the thread id is " << *pid[i] << endl; } cout << "main thread exit..." << endl; exit(0); }
实例5:创建多个线程(个数自己指定)同时向各个线程中传递一个整数值,并在主线程中等待这些线程,线程退出的时候向主线程返回值(将自身的线程id传给主线程),最要的是让线程脱离主线程;使用pthread_create, pthread_join, pthread_self, pthread_exit, pthread_detach
第一:出错的例子
/************************************************************************* > File Name: create_join.cpp > Author: > Mail: > Created Time: 2015年12月16日 星期三 10时41分19秒 ************************************************************************/ #include<iostream> #include <unistd.h> #include <pthread.h> #include <cstdlib> using namespace std; void *task(void *a) { int i = 0; int * loop; pthread_t pid = pthread_self(); pthread_detach(pid); loop = static_cast<int*> (a); for (i = 0; i < *loop; ++i){ cout << i+1 << ": This is thread..." << "thread id is " << pid << endl; sleep(1); } cout << "thread exit.." << pid << endl; pthread_exit((void *)&pid); } int main(int argc, char *argv[]) { int ret; int i = 0; if(argc != 2){ cout << "You must input the number of thread you want to create..." << endl; exit(-1); } int thread_num = atoi(argv[1]); pthread_t threadId[thread_num]; for (i = 0; i < thread_num; ++i){ ret = pthread_create(&threadId[i], NULL, task, (void *)&thread_num); if(ret != 0){ cout << "pthread_create error..." << endl; exit(-1); } } pthread_t * pid[thread_num]; for (i = 0; i < thread_num; ++i){ ret = pthread_join(threadId[i], (void **)&pid[i]); if(ret != 0){ cout << "pthread_join error..." << i+1 << endl; exit(-1); } } for (i = 0; i < thread_num; ++i){ cout << "main thread wait the thread id is " << *pid[i] << endl; } cout << "main thread exit..." << endl; exit(0); }
第二:正确的
/************************************************************************* > File Name: create_join.cpp > Author: > Mail: > Created Time: 2015年12月16日 星期三 10时41分19秒 ************************************************************************/ #include<iostream> #include <unistd.h> #include <pthread.h> #include <cstdlib> using namespace std; void *task(void *a) { int i = 0; int * loop; pthread_t pid = pthread_self(); pthread_detach(pid); loop = static_cast<int*> (a); for (i = 0; i < *loop; ++i){ cout << i+1 << ": This is thread..." << "thread id is " << pid << endl; sleep(1); } cout << "thread exit.." << pid << endl; } int main(int argc, char *argv[]) { int ret; int i = 0; if(argc != 2){ cout << "You must input the number of thread you want to create..." << endl; exit(-1); } int thread_num = atoi(argv[1]); pthread_t threadId[thread_num]; for (i = 0; i < thread_num; ++i){ ret = pthread_create(&threadId[i], NULL, task, (void *)&thread_num); if(ret != 0){ cout << "pthread_create error..." << endl; exit(-1); } } sleep(5);//必须等待,如果不等待则该进程退出,那么创建的线程也会退出 cout << "main thread exit..." << endl; exit(0); }
再说一个linux下写程序的一个核武器--------------man手册;