1.多线程1.创建多线程示例代码使用for循环去创建一个一个的线程让它们去循环的打印出自己的pid和线程tid创建完线程之后不要忘记去释放掉线程资源避免内存泄漏。#include iostream #include pthread.h #include unistd.h #include vector using namespace std; #define NUM 8 struct ThreadData { string threadname; }; string Tohex(pthread_t tid) { char buffer[128]; snprintf(buffer,sizeof(buffer),0x%lx,tid); return buffer; } void InitThreadname(ThreadData*td,int i) { td-threadnamethreadto_string(i); } void*threadRoutine(void*args) { ThreadData*tdstatic_castThreadData*(args); int i0; while(i10) { coutpid: getpid(), tid : Tohex(pthread_self()), threadname: td-threadnameendl; sleep(1); i; } delete td; return nullptr; } int main() { vectorpthread_t tids; for(int i0;iNUM;i) { pthread_t tid; //这样子写是不可以的理论上面这个是在主线程创建的所以还是创建是在主线程的栈上面for循环结束了也就销毁了 //ThreadData td; ThreadData*tdnew ThreadData;//创建在堆上面每次创建都访问的是不同的堆空间这样子每一个线程都原来属于自己的堆空间 InitThreadname(td,i); pthread_create(tid,nullptr,threadRoutine,td); tids.push_back(tid); } for(int i0;itids.size();i) { pthread_join(tids[i],nullptr); } }使用ps-aL来查看监控中的轻量化进程查看创建情况。while :; do ps -aL|head -1ps -aL|grep my|grep -v grep;sleep 1;done运行结果从左侧代码的运行结果可以看出来这些线程的pid都是一样的说明都是一个进程创建出来的右侧可以看出来pid一样但是LWP不一样说明确实瞬间创建出来了多个线程。ps这里也应该明白虽然线程好像是创建在不同的堆上面好像看样子是每一个线程都拥有了自己的空间互相不打扰但是线程之间是没有秘密的一个线程想要知道其他线程的数据是很简单的之前找到了tid是动态库存储线程数据的起始地址只要线程获取到了这个tid也就可以获取到数据了。2.线程中的变量和全局变量2.1 线程中创建的变量在单个线程中每个线程都是有自己的独立线程栈的所以在线程执行函数里面定义了一个变量这个变量会在每一个线程中存在一份。示例代码在线程执行函数里面定义一个变量查看线程中变量的地址和在对test_i加加两次后test_i的值。#include iostream #include pthread.h #include unistd.h #include vector using namespace std; #define NUM 8 struct ThreadData { string threadname; }; string Tohex(pthread_t tid) { char buffer[128]; snprintf(buffer,sizeof(buffer),0x%lx,tid); return buffer; } void InitThreadname(ThreadData*td,int i) { td-threadnamethreadto_string(i); } void*threadRoutine(void*args) { ThreadData*tdstatic_castThreadData*(args); int i0; int test_i0; while(i2) { coutpid: getpid(), tid : Tohex(pthread_self()), threadname: td-threadname test_i: test_iendl; sleep(1); i; test_i; } delete td; return nullptr; } int main() { vectorpthread_t tids; for(int i0;iNUM;i) { pthread_t tid; //这样子写是不可以的理论上面这个是在主线程创建的所以还是创建是在主线程的栈上面for循环结束了也就销毁了 //ThreadData td; ThreadData*tdnew ThreadData;//创建在堆上面每次创建都访问的是不同的堆空间这样子每一个线程都原来属于自己的堆空间 InitThreadname(td,i); pthread_create(tid,nullptr,threadRoutine,td); tids.push_back(tid); } for(int i0;itids.size();i) { pthread_join(tids[i],nullptr); } }运行结果可以看到有10个test_i的地址说明每一个线程中都存在这一个变量并且对自己的变量进行操作没有影响其他的线程。2.2 全局变量如果这个变量是全局变量的话那情况就不一样了因为全局变量只有一份线程肯定是看得到这个全局变量的所以在多线程中对这个变量进行操作就都是对这个全局变量进行操作了代码示例#include iostream #include pthread.h #include unistd.h #include vector using namespace std; #define NUM 8 int test_i0; struct ThreadData { string threadname; }; string Tohex(pthread_t tid) { char buffer[128]; snprintf(buffer,sizeof(buffer),0x%lx,tid); return buffer; } void InitThreadname(ThreadData*td,int i) { td-threadnamethreadto_string(i); } void*threadRoutine(void*args) { ThreadData*tdstatic_castThreadData*(args); int i0; while(i2) { coutpid: getpid(), tid : Tohex(pthread_self()), threadname: td-threadname test_i: test_iendl; sleep(1); i; test_i; } delete td; return nullptr; } int main() { vectorpthread_t tids; for(int i0;iNUM;i) { pthread_t tid; //这样子写是不可以的理论上面这个是在主线程创建的所以还是创建是在主线程的栈上面for循环结束了也就销毁了 //ThreadData td; ThreadData*tdnew ThreadData;//创建在堆上面每次创建都访问的是不同的堆空间这样子每一个线程都原来属于自己的堆空间 InitThreadname(td,i); pthread_create(tid,nullptr,threadRoutine,td); tids.push_back(tid); } for(int i0;itids.size();i) { pthread_join(tids[i],nullptr); } }运行结果可以看到所有的线程都是对全局变量进行操作像这种全局变量会被多个资源访问到那么这个变量就可以称为是被多个线程共享的‘共享资源’。3.__thread关键字两个___thread是GCC编译器内置的关键字正式名称叫线程局部存储Thread-Local StorageTLS使用__thread修饰的变量会在每一个线程中有一个独立的副本。ps这个是编译器内置的关键字所以只能修饰内置类型不能修饰自定义类型。#include iostream #include pthread.h #include unistd.h #include vector using namespace std; #define NUM 8 __thread int test_i0; struct ThreadData { string threadname; }; string Tohex(pthread_t tid) { char buffer[128]; snprintf(buffer,sizeof(buffer),0x%lx,tid); return buffer; } void InitThreadname(ThreadData*td,int i) { td-threadnamethreadto_string(i); } void*threadRoutine(void*args) { ThreadData*tdstatic_castThreadData*(args); int i0; while(i2) { coutpid: getpid(), tid : Tohex(pthread_self()), threadname: td-threadname test_i: test_iendl; sleep(1); i; test_i; } delete td; return nullptr; } int main() { vectorpthread_t tids; for(int i0;iNUM;i) { pthread_t tid; //这样子写是不可以的理论上面这个是在主线程创建的所以还是创建是在主线程的栈上面for循环结束了也就销毁了 //ThreadData td; ThreadData*tdnew ThreadData;//创建在堆上面每次创建都访问的是不同的堆空间这样子每一个线程都原来属于自己的堆空间 InitThreadname(td,i); pthread_create(tid,nullptr,threadRoutine,td); tids.push_back(tid); } for(int i0;itids.size();i) { pthread_join(tids[i],nullptr); } }运行结果可以发现确实在每一个线程中都单独存在了一个变量并且只对自己的线程中的变量进行操作。4. 线程访问其他线程的栈上面我们提到了在线程中是没有秘密的如果一个线程想要获取到其他线程的数据是可以做到的这里我们定义一个全局变量在主线程中对线程5的i值进行改变这样子线程5在while循环中只会打印两次其他线程则会打印3次。示例代码#include iostream #include pthread.h #include unistd.h #include vector using namespace std; #define NUM 8 int *pnullptr; struct ThreadData { string threadname; }; string Tohex(pthread_t tid) { char buffer[128]; snprintf(buffer,sizeof(buffer),0x%lx,tid); return buffer; } void InitThreadname(ThreadData*td,int i) { td-threadnamethread-to_string(i); } void*threadRoutine(void*args) { ThreadData*tdstatic_castThreadData*(args); int i0; if(td-threadnamethread-5) { pi; } while(i3) { coutpid: getpid(), tid : Tohex(pthread_self()), threadname: td-threadnameendl; sleep(1); i; } delete td; return nullptr; } int main() { vectorpthread_t tids; for(int i0;iNUM;i) { pthread_t tid; //这样子写是不可以的理论上面这个是在主线程创建的所以还是创建是在主线程的栈上面for循环结束了也就销毁了 //ThreadData td; ThreadData*tdnew ThreadData;//创建在堆上面每次创建都访问的是不同的堆空间这样子每一个线程都原来属于自己的堆空间 InitThreadname(td,i); pthread_create(tid,nullptr,threadRoutine,td); tids.push_back(tid); } sleep(2); *p2; for(int i0;itids.size();i) { pthread_join(tids[i],nullptr); } }运行结果5. 线程分离创建一个线程就是创建一个PCB所以对于线程来说主线程也是要等待其他线程的但是如果线程一直不退出那主线程不是就一直卡住了吗如果主线程要去做其他的事情不就也做不了了。如果我们对于线程的退出结果并不关心就可以让操作系统帮我们管理线程线程执行完了直接退出由操作系统来释放该资源。#include pthread.h int pthread_detach(pthread_t thread);thread参数线程的tid。代码示例1主线程在创建线程完成后分离线程但是不退出最后我们应该通过监控脚本可以看到3秒后只剩下主线程还在运行其他线程已经退出被操作系统释放。#include iostream #include pthread.h #include unistd.h #include vector using namespace std; #define NUM 8 struct ThreadData { string threadname; }; string Tohex(pthread_t tid) { char buffer[128]; snprintf(buffer,sizeof(buffer),0x%lx,tid); return buffer; } void InitThreadname(ThreadData*td,int i) { td-threadnamethread-to_string(i); } void*threadRoutine(void*args) { ThreadData*tdstatic_castThreadData*(args); int i0; while(i3) { coutpid: getpid(), tid : Tohex(pthread_self()), threadname: td-threadnameendl; sleep(1); i; } delete td; return nullptr; } int main() { vectorpthread_t tids; for(int i0;iNUM;i) { pthread_t tid; //这样子写是不可以的理论上面这个是在主线程创建的所以还是创建是在主线程的栈上面for循环结束了也就销毁了 //ThreadData td; ThreadData*tdnew ThreadData;//创建在堆上面每次创建都访问的是不同的堆空间这样子每一个线程都原来属于自己的堆空间 InitThreadname(td,i); pthread_create(tid,nullptr,threadRoutine,td); tids.push_back(tid); } for(int i0;itids.size();i) { pthread_detach(tids[i]); } while(true) {} return 0; }运行结果示例代码2线程自己也可以获取到自己的tid所以也可以在线程内部调用pthread_detach让线程分离。#include iostream #include pthread.h #include unistd.h #include vector using namespace std; #define NUM 8 struct ThreadData { string threadname; }; string Tohex(pthread_t tid) { char buffer[128]; snprintf(buffer,sizeof(buffer),0x%lx,tid); return buffer; } void InitThreadname(ThreadData*td,int i) { td-threadnamethread-to_string(i); } void*threadRoutine(void*args) { pthread_detach(pthread_self()); ThreadData*tdstatic_castThreadData*(args); int i0; while(i3) { coutpid: getpid(), tid : Tohex(pthread_self()), threadname: td-threadnameendl; sleep(1); i; } delete td; return nullptr; } int main() { vectorpthread_t tids; for(int i0;iNUM;i) { pthread_t tid; //这样子写是不可以的理论上面这个是在主线程创建的所以还是创建是在主线程的栈上面for循环结束了也就销毁了 //ThreadData td; ThreadData*tdnew ThreadData;//创建在堆上面每次创建都访问的是不同的堆空间这样子每一个线程都原来属于自己的堆空间 InitThreadname(td,i); pthread_create(tid,nullptr,threadRoutine,td); tids.push_back(tid); } // for(int i0;itids.size();i) // { // pthread_detach(tids[i]); // } while(true) {} return 0; }示例代码3如果让线程等待了又分离线程会怎么样呢#include iostream #include pthread.h #include unistd.h #include vector using namespace std; #define NUM 8 struct ThreadData { string threadname; }; string Tohex(pthread_t tid) { char buffer[128]; snprintf(buffer,sizeof(buffer),0x%lx,tid); return buffer; } void InitThreadname(ThreadData*td,int i) { td-threadnamethread-to_string(i); } void*threadRoutine(void*args) { // pthread_detach(pthread_self()); ThreadData*tdstatic_castThreadData*(args); int i0; while(i3) { coutpid: getpid(), tid : Tohex(pthread_self()), threadname: td-threadnameendl; sleep(1); i; } delete td; return nullptr; } int main() { vectorpthread_t tids; for(int i0;iNUM;i) { pthread_t tid; //这样子写是不可以的理论上面这个是在主线程创建的所以还是创建是在主线程的栈上面for循环结束了也就销毁了 //ThreadData td; ThreadData*tdnew ThreadData;//创建在堆上面每次创建都访问的是不同的堆空间这样子每一个线程都原来属于自己的堆空间 InitThreadname(td,i); pthread_create(tid,nullptr,threadRoutine,td); tids.push_back(tid); } for(int i0;itids.size();i) { pthread_join(tids[i],nullptr); } for(int i0;itids.size();i) { pthread_detach(tids[i]); } return 0; }运行结果会出现段错误的问题显然分离和等待只能二选一。ps线程分离也就是获取到了pthread库中存储的线程描述符tcb的起始地址tcb中有一个字段表示该线程是否分离将其改为1表示分离状态。