线程的终止
之前讲过一个exit的函数,这个函数是让进程终止,而不是线程,如果当前进程中任何一个执行流调用了exit函数,那么当前进程都会退出。
那么如何终止一个线程呢?函数中的return可以,还有一个函数可以:
参数默认设置为nullptr就可以了。
#include <iostream> #include <pthread.h> #include <cassert> #include <string> #include <vector> #include <unistd.h> #include <cstdlib> using namespace std; struct ThreadData { pthread_t tid; char buffer[64]; }; void *pthread_routine(void* args) { ThreadData* p = static_cast<ThreadData*>(args); while(true) { cout << "new " << p->buffer << endl; sleep(1); pthread_exit(nullptr);//线程终止 } delete p; } int main() { #define NUM 10 for(int i = 0; i < NUM; i++) { ThreadData *p = new ThreadData; char buffer[64]; snprintf(p->buffer,sizeof(p->buffer),"%s:%d","thread",i); pthread_create(&p->tid, nullptr, pthread_routine, p); //sleep(1); } while(true) { cout << "main thread" << endl; sleep(1); } return 0; }
线程的等待
线程也是要被等待的,如果不等待会造成类似于僵尸进程的问题——内存泄漏。
线程被等待的原因:
1.获取新线程退出的信息。
2.回收新线程对应PCB内核资源等,防止内存泄漏。(这里暂时无法查看)
这是等待线程的函数:
第一个参数是线程的id,第二个参数暂时设置为nullptr。
#include <iostream> #include <pthread.h> #include <cassert> #include <string> #include <vector> #include <unistd.h> #include <cstdlib> using namespace std; struct ThreadData { pthread_t tid; char buffer[64]; }; void *pthread_routine(void* args) { ThreadData* p = static_cast<ThreadData*>(args); while(true) { cout << "new " << p->buffer << endl; sleep(1); pthread_exit(nullptr); } } int main() { vector<ThreadData*> arr; #define NUM 10 for(int i = 0; i < NUM; i++) { ThreadData *p = new ThreadData;//这里每个创建的起始地址都不相同 char buffer[64]; snprintf(p->buffer,sizeof(p->buffer),"%s:%d","thread",i); pthread_create(&p->tid, nullptr, pthread_routine, p); arr.push_back(p); //sleep(1); } for(auto& e : arr) { cout << "等待线程id:" << e->tid << "成功" << endl; int n = pthread_join(e->tid, nullptr);//阻塞等待 assert(n==0); delete e; } while(true) { cout << "main thread" << endl; sleep(1); } return 0; }
那么,该函数的第二个参数是什么呢?其实是线程的返回值。(输出型参数)
这里返回的是一个void*,输出型参数用一个void**才能传回。
#include <iostream> #include <pthread.h> #include <cassert> #include <string> #include <vector> #include <unistd.h> #include <cstdlib> using namespace std; struct ThreadData { int num;//线程的编号 pthread_t tid; char buffer[64]; }; void *pthread_routine(void* args) { ThreadData* p = static_cast<ThreadData*>(args); while(true) { cout << "new " << p->buffer << endl; sleep(1); pthread_exit(nullptr); } return (void*)p->num;//这里返回的是一个整数,这里强制转换成void*就相当于将四字节的内容写入了八字节的空间里,void* ret = (void*)p->num; } int main() { vector<ThreadData*> arr; #define NUM 10 for(int i = 0; i < NUM; i++) { ThreadData *p = new ThreadData; p->num = i+1; char buffer[64]; snprintf(p->buffer,sizeof(p->buffer),"%s:%d","thread",i); pthread_create(&p->tid, nullptr, pthread_routine, p); arr.push_back(p); //sleep(1); } for(auto& e : arr) { void* ret = nullptr;//注意,这里是void* int n = pthread_join(e->tid, &ret);//这里传过去的是ret的地址,等待传回来的返回值是void*的,就等于直接写入的ret当中 assert(n==0); cout << "等待线程:" << e->buffer << "成功"<< "编号:" << (long long)e->num << endl;//因为是64位系统,需要转换成longlong类型 delete e; } while(true) { cout << "main thread" << endl; sleep(1); } return 0; }
同理,线程中再堆区开辟的空间地址也可以拿到。
线程取消
注意:线程取消的前提是线程已经在运行了。
参数是线程id。
#include <iostream> #include <pthread.h> #include <cassert> #include <string> #include <vector> #include <unistd.h> #include <cstdlib> using namespace std; struct ThreadData { int num;//线程的编号 pthread_t tid; char buffer[64]; }; void *pthread_routine(void* args) { ThreadData* p = static_cast<ThreadData*>(args); while(true) { cout << "new " << p->buffer << endl; sleep(1); } return (void*)100;//正常退出就返回100 } int main() { vector<ThreadData*> arr; #define NUM 10 for(int i = 0; i < NUM; i++) { ThreadData *p = new ThreadData; p->num = i+1; char buffer[64]; snprintf(p->buffer,sizeof(p->buffer),"%s:%d","thread",i); pthread_create(&p->tid, nullptr, pthread_routine, p); arr.push_back(p); //sleep(1); } sleep(3); for(auto& e : arr) { pthread_cancel(e->tid); cout << "取消的线程id:" << e->tid << "成功" << endl; } for(auto& e : arr) { void* ret = nullptr; int n = pthread_join(e->tid, &ret); assert(n==0); cout << "等待线程:" << e->buffer << "成功"<< "编号:" << (long long)ret << endl;//因为是64位系统,需要转换成longlong类型 delete e; } while(true) { cout << "main thread" << endl; sleep(1); } return 0; }
线程一旦被取消,退出码就是-1。