平常创建线程建议案例:
void* fun(void* arg) { for(int i = 0; i < 5; i++) { cout << " 子线程 i = " << i << endl; } // 这个函数什么都不做,只是设置一个线程取消点(如果有死循环,放在死循环里) pthread_testcancel(); return NULL; } int main() { int ret = pthread_create(&tid, &attr, fun, NULL); if(ret != 0) { // 设置线程分离 pthread_detach(tid); } return 0; }
#include <iostream> #include <pthread.h> #include <sys/types.h> #include <unistd.h> #include <string.h> using namespace std; /**** 1、创建线程最简单的例子 ****/ void* fun1(void* arg) { cout << "线程 tid = " << pthread_self() << endl; return NULL; } void test1() { pthread_t tid; pthread_create(&tid, NULL, fun1, NULL); cout << "PID = " << getpid() << endl; sleep(1); } /**** 2、循环创建5个线程,并在线程里面确定是第几个被创建的 ****/ void* fun2(void* arg) { long num = (long)arg; sleep(num); cout << "线程 tid = " << pthread_self() << ", num = " << num + 1 << endl; return NULL; } void test2() { for(int i = 0; i < 5; ++i) { pthread_t tid; pthread_create(&tid, NULL, fun2, (void*)i); } sleep(5); cout << "我是主线程,pid = " << getpid() << ", << tid = " << pthread_self() << endl; cout << "sizeof(long) = " << sizeof(long) << endl; } /**** 3、线程创建失败,打印错误信息的例子 ****/ void* fun3(void* arg) { cout << "线程 tid = " << pthread_self() << endl; return NULL; } void test3() { pthread_t tid; int ret = pthread_create(&tid, NULL, fun3, NULL); if(ret != 0) { cout << "pthread_create error: " << strerror(ret); } cout << "PID = " << getpid() << endl; sleep(1); } /**** 4、pthread_exit 案例 ****/ void* fun4(void* arg) { cout << "线程 tid = " << pthread_self() << endl; for(int i = 0; i < 5; ++i) { cout << "子线程 i = " << i << endl; } return NULL; } void test4() { pthread_t tid; int ret = pthread_create(&tid, NULL, fun4, NULL); if(ret != 0) { cout << "pthread_create error: " << strerror(ret); } sleep(3); // 退出主线程 不影响其它线程(记住) pthread_exit(NULL); for(int i = 0; i < 5; ++i) { cout << "主线程 i = " << i << endl; } } /**** 5、pthread_join(阻塞等待线程退出,获取线程退出状态) 案例 ****/ int number = 100; void* fun5(void* arg) { cout << "线程 tid = " << pthread_self() << endl; for(int i = 0; i < 5; ++i) { cout << "子线程 i = " << i << endl; } pthread_exit(&number); return NULL; } void test5() { pthread_t tid; int ret = pthread_create(&tid, NULL, fun5, NULL); if(ret != 0) { // 线程创建失败,打印错误信息 cout << "pthread_create error: " << strerror(ret); } else { // 阻塞等待线程退出,并回收PCB void *ptr = NULL; pthread_join(tid, &ptr); int a = *(int*)ptr; cout << " number = " << a << endl; } for(int i = 0; i < 10; ++i) { cout << "主线程 i = " << i << endl; } } /**** 6、pthread_cancel 案例 ****/ void* fun6(void* arg) { int i = 0; while(true) { i++; // 这个函数什么都不做,只是设置一个线程取消点 pthread_testcancel(); } return NULL; } void test6() { pthread_t tid; int ret = pthread_create(&tid, NULL, fun6, NULL); if(ret != 0) { // 线程创建失败,打印错误信息 cout << "pthread_create error: " << strerror(ret); } else { // 结束子线程(线程里面必须有一次系统调用,否则该函数不起作用) pthread_cancel(tid); pthread_join(tid, NULL); } } /**** 7、设置线程分离(设置线程分离之后,线程自动回收PCB) 案例 ****/ // 非分离状态:线程的默认属性是非分离状态,这种情况下,原有的线程等待创建的线程结束。只有当pthread_join()函数返回时,创建的线程才算终止,才能释放自己占用的系统资源。 // 分离状态:分离线程没有被其他的线程所等待,自己运行结束了,线程也就终止了,马上释放系统资源。应该根据自己的需要,选择适当的分离状态。 // 注意:不能对一个已经处于 detach 状态的线程调用 pthread_join ,这样的调用将返回 EINVAL 。如果已经对一个线程调用了pthread_detach 就不能再调用 pthread_join 了。 void* fun7(void* arg) { for(int i = 0; i < 5; i++) { cout << " 子线程 i = " << i << endl; } // 这个函数什么都不做,只是设置一个线程取消点 pthread_testcancel(); return NULL; } void test7() { // 初始化线程的属性 pthread_attr_t attr; pthread_attr_init(&attr); // 设置分离 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); pthread_t tid; // 创建线程的时候,设置线程分离 int ret = pthread_create(&tid, &attr, fun7, NULL); if(ret != 0) { #if 0 // 这种方法也可以设置线程分离 pthread_detach(tid); #endif // 线程创建失败,打印错误信息 cout << "pthread_create error: " << strerror(ret); } else { for(int i = 0; i < 5; i++) { cout << " 主线程 i = " << i << endl; } } // 释放资源 pthread_attr_destroy(&attr); sleep(3); } int main() { test7(); return 0; }