赞
踩
我们知道Linux没有很明确的线程的概念,只有轻量级进程的概念。
Linux不会直接提供线程的系统调用,只会给我们提供轻量级进程的系统调用。
但是我们用户需要线程的接口!
可是我们用户又不能直接调用系统调用去创建线程,那怎么创建线程呢?
Linux的程序员们就在系统调用和用户之间搭建了一个桥梁,那就是pthread线程库
这个库是在应用层的,它把我们的轻量级进程的接口进行封装,就能为用户提供线程的接口
大部分Linux系统都会默认带上该线程库。
POSIX线程库是一套标准,而pthread库是这套标准的实现。
pthread线程库是应用层的原生线程库:
错误检查:
创建线程的函数叫做pthread_create
pthread_create函数的函数原型如下:
参数说明:
返回值说明:
void 能定义变量吗?不可以
void*能定义变量吗?可以,通常用来当函数的返回类型,比如malloc的返回值
我们这个64位环境下面是8字节
当一个程序启动时,就有一个进程被操作系统创建,与此同时一个线程也立刻运行,这个线程就叫做主线程。
下面我们让主线程调用pthread_create函数创建一个新线程,此后新线程就会跑去执行自己的新例程,而主线程则继续执行后续代码。
- #include <stdio.h>
- #include <pthread.h>
- #include <unistd.h>
-
- void* Routine(void* arg)
- {
- char* msg = (char*)arg;
- while (1){
- printf("I am %s , %d\n", msg,getpid());
- sleep(1);
- }
- return (void*)0;
- }
- int main()
- {
- pthread_t tid;
- pthread_create(&tid, NULL, Routine, (void*)"thread 1");
- while (1){
- printf("I am main thread : %d!\n",getpid());
- sleep(2);
- }
- return 0;
- }

我们进行编译
找不到这个库???
这个是第三方库,跟我们自己制作的库的使用方法是一样的。我们需要链接这些线程函数库时,要使用编译器命令的“-lpthread”选项。
我们make一下
怎么样?
运行代码后可以看到,新线程每隔一秒执行一次打印操作,而主线程每隔两秒执行一次打印操作。
当我们在另外一个账号用ps axj
命令查看当前进程的信息时,虽然此时该进程中有两个线程,但是我们看到的进程只有一个,因为这两个线程都是属于同一个进程的。
使用ps -aL
命令,可以显示当前的轻量级进程。
-L
,看到的就是一个个的进程。-L
就可以查看到每个进程内的多个轻量级进程。ps -aL | head -1&&ps -aL | grep test | grep -v grep
其中,LWP(Light Weight Process)就是轻量级进程的ID,可以看到显示的两个轻量级进程的PID是相同的,因为它们属于同一个进程。
注意: 在Linux中,应用层的线程与内核的LWP是一一对应的,实际上操作系统调度的时候采用的是LWP,而并非PID,只不过我们之前接触到的都是单线程进程,其PID和LWP是相等的,所以对于单线程进程来说,调度时采用PID和LWP是一样的。
注意一下:上面两个线程的LWP和PID,有没有发现PID是211092的那个线程的LWP也是211092,所以它是主线程!!!! cpu就能通过这个来确认某个线程是不是主线程了。也可以进行更好的管理
比如我们杀进程的时候应该杀主线程
我们再运行一次
会是什么结果呢?
还是被干掉了
任何一个线程被杀掉了,整个进程都会退掉!!!!
我们接着看
线程之间是不是共享在栈上开辟的内存
#include <iostream> #include <pthread.h> #include <unistd.h> #include<string> using namespace std; void show(const string&str) { cout<<str<<"say #"<<endl; } void* Routine(void* arg) { char* msg = (char*)arg; while (1){ show("Routine"); printf("I am %s , %d\n", msg,getpid()); sleep(1); } return (void*)0; } int main() { pthread_t tid; pthread_create(&tid, NULL, Routine, (void*)"thread 1"); while (1){ show("main"); printf("I am main thread : %d!\n",getpid()); sleep(2); } return 0; }
我们发现同一个方法可以被多个执行流调度!!!
我们接下来来定义一个全局变量
#include <iostream> #include <pthread.h> #include <unistd.h> #include<string> using namespace std; int cnt=0; void* Routine(void* arg) { char* msg = (char*)arg; while (1){ cout<<"cnt:"<<cnt<<" &cnt:"<<&cnt<<endl; printf("I am %s , %d\n", msg,getpid()); sleep(1); } return (void*)0; } int main() { pthread_t tid; pthread_create(&tid, NULL, Routine, (void*)"thread 1"); while (1){ cnt++; cout<<"cnt:"<<cnt<<" &cnt:"<<&cnt<<endl;; printf("I am main thread : %d!\n",getpid()); sleep(2); } return 0; }
我们发现多个线程看到的全局变量是一样的,因为它们共享地址空间
我们再看看堆的情况
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<pthread.h> void*ThreadEntry(void*arg) { int*ptr=(int*)arg; int n=0; *ptr=100; while(1) { if(n++>3) pthread_exit(NULL);//如果n>3,那此线程就退出 printf("I am thread,thread p is %d......\n",*ptr); sleep(1); } } int main() { int count=9999; int *ptr=&count; pthread_t tid; pthread_create(&tid,NULL,ThreadEntry,(void*)ptr); printf("mian ptr=%d\n",*ptr); while(1) { pthread_join(tid,NULL);//等待新线程执行结束,才会执行自己 printf("I am main,main ptr is %d\n",*ptr); sleep(1); } free(ptr); return 0; }
结果解析:
在主线程中开辟了一块内存,并给此内存赋值,输出此值—>9999,创建新线程,并在新线程中修改在主线程开辟的那块内存的内容,主线程等待(pthread_join)新线程结束,再去执行自己,此时输出的ptr和新线程中的是一样的结果,故线程之间共享在堆上的空间
综上所说,同一个进程的多个线程的资源都是共享的,线程之间要是想通信,好简单!!!!
接下来再来看看出现异常的情况
#include <iostream> #include <pthread.h> #include <unistd.h> #include<string> using namespace std; int cnt=0; void* Routine(void* arg) { char* msg = (char*)arg; while (1){ cout<<"cnt:"<<cnt<<" &cnt:"<<&cnt<<endl; printf("I am %s , %d\n", msg,getpid()); sleep(1); } return (void*)0; } int main() { pthread_t tid; pthread_create(&tid, NULL, Routine, (void*)"thread 1"); while (1){ cnt++; cout<<"cnt:"<<cnt<<" &cnt:"<<&cnt<<endl;; printf("I am main thread : %d!\n",getpid()); sleep(2); int a=10; a/=0;//注意这里 } return 0; }
全部线程都被杀掉了
如果我把除0错误代码放到新线程这里看看
也是全杀掉了
综上
各个线程共享全局变量,全局函数,堆内存
上面是让主线程创建一个新线程,下面我们让主线程一次性创建五个新线程,并让创建的每一个新线程都去执行Routine函数,也就是说Routine函数会被重复进入,即该函数是会被重入的。
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <sys/types.h>
-
- void* Routine(void* arg)
- {
- char* msg = (char*)arg;
- while (1){
- printf("I am %s...pid: %d, ppid: %d\n", msg, getpid(), getppid());
- sleep(1);
- }
- }
-
- int main()
- {
- pthread_t tid[5];
- for (int i = 0; i < 5; i++){
- char* buffer = (char*)malloc(64);
- sprintf(buffer, "thread %d", i);
- pthread_create(&tid[i], NULL, Routine, buffer);
- }
- while (1){
- printf("I am main thread...pid: %d, ppid: %d\n", getpid(), getppid());
- sleep(2);
- }
- return 0;
- }

运行代码,可以看到这五个新线程是创建成功的。
因为主线程和五个新线程都属于同一个进程,所以它们的PID和PPID也都是一样的。
此时我们再用ps -aL
命令查看,就会看到六个轻量级进程。
常见获取线程ID的方式有两种:
我们先试试第一种方法
#include <iostream> #include <pthread.h> #include <unistd.h> #include<string> using namespace std; void* Routine(void* arg) { char* msg = (char*)arg; while (1){ printf("I am %s , %d\n", msg,getpid()); sleep(1); } return (void*)0; } int main() { pthread_t tid; pthread_create(&tid, NULL, Routine, (void*)"thread 1"); while (1){ printf("I am main thread : %d, new pthread tid:%p!\n",getpid(),tid); sleep(2); } return 0; }打印出来的是这个玩意
这个和我预想中的怎么不一样,不应该是下面这种吗
不急,我们在文章的最后一节来讲,这里可以先告诉你
我们获得的是用户级原生线程库的线程ID,而LWP是内核的轻量级进程ID,它们之间是一对一的关系。
来看看第2种方法
pthread_self函数的函数原型如下:
pthread_t pthread_self(void);
调用pthread_self函数即可获得当前线程的ID,类似于调用getpid函数获取当前进程的ID。
例如,下面代码中在新线程被创建后,主线程都将通过输出型参数获取到的线程ID进行打印,新线程又通过调用pthread_self函数获取到自身的线程ID进行打印。
#include <iostream> #include <pthread.h> #include <unistd.h> #include<string> using namespace std; void* threadrun(void* arg) { char* msg = (char*)arg; while(1) { printf("tread : %lu\n",pthread_self()); sleep(1); } } int main() { pthread_t tid; pthread_create(&tid,nullptr,threadrun,(void*)"thread 1"); printf("main thread creat thread done, new thread tid : %lu\n",tid); sleep(20); }运行代码,可以看到这两种方式获取到的线程的ID是一样的。
注意: 用pthread_self函数获得的线程ID与内核的LWP的值是不相等的,pthread_self函数获得的是用户级原生线程库的线程ID,而LWP是内核的轻量级进程ID,它们之间是一对一的关系。
我们完全可以把这个过程类似于进程等待
等待线程的函数叫做pthread_join
pthread_join函数的函数原型如下:
参数说明:
返回值说明:
调用该函数的线程将挂起等待,直到ID为thread的线程终止,thread线程以不同的方法终止,通过pthread_join得到的终止状态是不同的。
总结如下:
我们看例子
- #include <iostream>
- #include <pthread.h>
- #include <unistd.h>
- #include<string>
- using namespace std;
-
-
- void* Routine(void* arg)
- {
- char* msg = (char*)arg;
- int cnt=0;
- while (1){
- printf("I am %s , %d\n", msg,getpid());
- sleep(1);
- cnt++;
- if(cnt==5) break;
- }
- return (void*)0;
- }
- int main()
- {
- pthread_t tid;
- pthread_create(&tid, NULL, Routine, (void*)"thread 1");
-
- pthread_join(tid,nullptr);
-
- cout<<"main thread quit ...."<<endl;
-
- return 0;
- }

while :; do ps -aL | head -1 && ps -aL | grep test | grep -v grep; sleep 1; echo "______________";done
我们运行一下
新线程不退,主线程就不退,主线程会阻塞等待!!!
注意: pthread_join函数默认是以阻塞的方式进行线程等待的。
接下来我们就来获取退出码了啊
我们先来理解这个pthread_join函数的第二个参数:(retval:线程退出时的退出码信息)
要通过函数的参数返回一个值,您需要传入变量的地址以接收新值。
因为线程退出码可不是一个int类型的,而是void*类型的!!!而pthread_join函数的第二个参数:retval是个传入参数,是要传进去获取退出码消息的,所以需要将参数设置为void**类型
下面我们再来看看如何获取线程退出时的退出码,为了便于查看,我们这里将线程退出时的退出码设置为某个特殊的值,并在成功等待线程后将该线程的退出码进行输出。
- #include <iostream>
- #include <pthread.h>
- #include <unistd.h>
- #include<string>
- using namespace std;
-
-
- void* Routine(void* arg)
- {
- char* msg = (char*)arg;
- int cnt=0;
- while (1){
- printf("I am %s , %d\n", msg,getpid());
- sleep(1);
- cnt++;
- if(cnt==5) break;
- }
- return (void*)1;
- }
- int main()
- {
- pthread_t tid;
- pthread_create(&tid, NULL, Routine, (void*)"thread 1");
-
- void* ret = nullptr;
- pthread_join(tid, &ret);
- printf("thread 1...quit, exitcode: %d\n",ret);
-
- cout<<"main thread quit ...."<<endl;
-
- return 0;
- }

注意:64位机器下面void*是8字节,int是4字节,它们不能进行强制类型转换!!!! 我们可以使用long long去看看
运行代码,此时我们就拿到了线程退出时的退出码信息。
3.1.1.为什么线程退出时只能拿到线程的退出码?
如果我们等待的是一个进程,那么当这个进程退出时,我们可以通过wait函数或是waitpid函数的输出型参数status,获取到退出进程的退出码、退出信号以及core dump标志。
那为什么等待线程时我们只能拿到退出线程的退出码?难道线程不会出现异常吗?
线程在运行过程中当然也会出现异常,线程和进程一样,线程退出的情况也有三种:
因此我们也需要考虑线程异常终止的情况,但是pthread_join函数无法获取到线程异常退出时的信息。因为线程是进程内的一个执行分支,如果进程中的某个线程崩溃了,那么整个进程也会因此而崩溃,此时我们根本没办法执行pthread_join函数,因为整个进程已经退出了。
例如,我们在线程的执行例程当中制造一个除零错误,当某一个线程执行到此处时就会崩溃,进而导致整个进程崩溃。
- #include <iostream>
- #include <pthread.h>
- #include <unistd.h>
- #include<string>
- using namespace std;
-
-
- void* Routine(void* arg)
- {
- char* msg = (char*)arg;
- int cnt=0;
- while (1){
- printf("I am %s , %d\n", msg,getpid());
- sleep(1);
- cnt++;
- if(cnt==5) break;
- }
- int n=5/0;//除0错误
- return (void*)1;
- }
- int main()
- {
- pthread_t tid;
- pthread_create(&tid, NULL, Routine, (void*)"thread 1");
-
- void* ret = nullptr;
- pthread_join(tid, &ret);
- printf("thread 1...quit, exitcode: %d\n",ret);
-
- cout<<"main thread quit ...."<<endl;
-
- return 0;
- }

运行代码,可以看到一旦某个线程崩溃了,整个进程也就跟着挂掉了,此时主线程连等待新线程的机会都没有,这也说明了多线程的健壮性不太强,一个进程中只要有一个线程挂掉了,那么整个进程就挂掉了。
所以pthread_join函数只能获取到线程正常退出时的退出码,用于判断线程的运行结果是否正确。
如果需要只终止某个线程而不是终止整个进程,可以有三种方法:
在介绍那些之前,我先来看看exit函数
- #include <iostream>
- #include <pthread.h>
- #include <unistd.h>
- #include<string>
- using namespace std;
-
-
- void* Routine(void* arg)
- {
- char* msg = (char*)arg;
- int cnt=0;
- while (1){
- printf("I am %s , %d\n", msg,getpid());
- sleep(1);
- cnt++;
- if(cnt==5) break;
- }
- exit(0);
-
- }
- int main()
- {
- pthread_t tid;
- pthread_create(&tid, NULL, Routine, (void*)"thread 1");
-
- void* ret = nullptr;
- pthread_join(tid, &ret);
- printf("thread 1...quit, exitcode: %d\n",ret);
-
- sleep(7);
- cout<<"main thread quit ...."<<endl;
-
- return 0;
- }

好家伙,在新线程里面使用exit(),整个进程都退掉了!!!
注意: exit函数的作用是终止进程,任何一个线程调用exit函数也代表的是整个进程终止。
在线程中使用return代表当前线程退出,但是在main函数中使用return代表整个进程退出,也就是说只要主线程退出了那么整个进程就退出了,此时该进程曾经申请的资源就会被释放,而其他线程会因为没有了资源,自然而然的也退出了。
例如,在下面代码中,主线程创建五个新线程后立刻进行return,那么整个进程也就退出了。
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <sys/types.h>
-
- void* Routine(void* arg)
- {
- char* msg = (char*)arg;
- while (1){
- printf("I am %s...pid: %d, ppid: %d, tid: %lu\n", msg, getpid(), getppid(), pthread_self());
- sleep(1);
- }
- return (void*)0;
- }
- int main()
- {
- pthread_t tid[5];
- for (int i = 0; i < 5; i++){
- char* buffer = (char*)malloc(64);
- sprintf(buffer, "thread %d", i);
- pthread_create(&tid[i], NULL, Routine, buffer);
- printf("%s tid is %lu\n", buffer, tid[i]);
- }
- printf("I am main thread...pid: %d, ppid: %d, tid: %lu\n", getpid(), getppid(), pthread_self());
-
- return 0;
- }

运行代码,并不能看到新线程执行的打印操作,因为主线程的退出导致整个进程退出了。
pthread_exit函数的功能就是终止线程,pthread_exit函数的函数原型如下:
void pthread_exit(void *retval);
参数说明:
说明一下:
例如,在下面代码中,我们使用pthread_exit函数终止线程,并将线程的退出码设置为6666。
- #include <iostream>
- #include <pthread.h>
- #include <unistd.h>
- #include<string>
- using namespace std;
-
-
- void* Routine(void* arg)
- {
- char* msg = (char*)arg;
- int cnt=0;
- while (1){
- printf("I am %s , %d\n", msg,getpid());
- sleep(1);
- cnt++;
- if(cnt==5) break;
- }
- pthread_exit((void*)10);
-
- }
- int main()
- {
- pthread_t tid;
- pthread_create(&tid, NULL, Routine, (void*)"thread 1");
-
- void* ret = nullptr;
- pthread_join(tid, &ret);
- printf("thread 1...quit, exitcode: %d\n",ret);
-
- sleep(2);
- cout<<"main thread quit ...."<<endl;
-
- return 0;
- }

运行代码可以看到,当线程退出时其退出码就是我们设置的10。
很好
线程是可以被取消的,我们可以使用pthread_cancel函数取消某一个线程,pthread_cancel函数的函数原型如下:
int pthread_cancel(pthread_t thread);
参数说明:
返回值说明:
线程是可以取消的,取消成功的线程的退出码一般是-1。
例如在下面的代码中,我们让主线程后将新线程取消。
- #include <iostream>
- #include <pthread.h>
- #include <unistd.h>
- #include<string>
- using namespace std;
-
-
- void* Routine(void* arg)
- {
- char* msg = (char*)arg;
- int cnt=0;
- while (1){
- printf("I am %s , %d\n", msg,getpid());
- sleep(1);
- cnt++;
- if(cnt==5)
- {
- break;
- }
-
- }
- pthread_exit((void*)10);
-
- }
- int main()
- {
- pthread_t tid;
- pthread_create(&tid, NULL, Routine, (void*)"thread 1");
-
- sleep(1);//确保线程存在
- pthread_cancel(tid);
-
- void* ret = nullptr;
- pthread_join(tid, &ret);
- printf("thread 1...quit, exitcode: %d\n",ret);
- cout<<"main thread quit ...."<<endl;
-
- return 0;
- }

运行代码,可以看到每个线程执行一次打印操作后就退出了,其退出码不是我们设置的10而是-1,因为我们是在线程执行pthread_exit函数前将线程取消的。
此外,新线程也是可以取消主线程的,例如下面我们让每一个线程都尝试对主线程进行取消。
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <sys/types.h>
-
- pthread_t main_thread;
-
- void* Routine(void* arg)
- {
- char* msg = (char*)arg;
- int count = 0;
- while (count < 5){
- printf("I am %s...pid: %d, ppid: %d, tid: %lu\n", msg, getpid(), getppid(), pthread_self());
- sleep(1);
- count++;
- pthread_cancel(main_thread);
- }
- pthread_exit((void*)6666);
- }
- int main()
- {
- main_thread = pthread_self();
- pthread_t tid[5];
- for (int i = 0; i < 5; i++){
- char* buffer = (char*)malloc(64);
- sprintf(buffer, "thread %d", i);
- pthread_create(&tid[i], NULL, Routine, buffer);
- printf("%s tid is %lu\n", buffer, tid[i]);
- }
- printf("I am main thread...pid: %d, ppid: %d, tid: %lu\n", getpid(), getppid(), pthread_self());
- for (int i = 0; i < 5; i++){
- void* ret = NULL;
- pthread_join(tid[i], &ret);
- printf("thread %d[%lu]...quit, exitcode: %d\n", i, tid[i], (int)ret);
- }
- return 0;
- }

运行代码,同时用以下监控脚本进行实时监控。
while :; do ps -aL | head -1&&ps -aL | grep test | grep -v grep;echo "###############";sleep 1;done
可以看到一段时间后,PID和LWP相同的线程,也就是主线程右侧显示<defunct>
,也就意味着主线程已经被取消了,我们也就看不到后续主线程等待新线程时打印的退出码了。
注意:
- 当采用这种取消方式时,主线程和各个新线程之间的地位是对等的,取消一个线程,其他线程也是能够跑完的,只不过主线程不再执行后续代码了。
- 我们一般都是用主线程去控制新线程,这才符合我们对线程控制的基本逻辑,虽然实验表明新线程可以取消主线程,但是并不推荐该做法。
一般情况下我们的代码结构就是下面这样子
- #include <iostream>
- #include <pthread.h>
- #include <unistd.h>
- #include<string>
- using namespace std;
-
- void* Routine(void* arg)
- {
-
- pthread_exit((void*)10);
-
- }
- int main()
- {
- pthread_t tid;
- pthread_create(&tid, NULL, Routine, (void*)"thread 1");
-
-
- void* ret = nullptr;
- pthread_join(tid, &ret);
- }

- 什么是线程分离(WHAT)
简单来讲,线程分离就是当线程被设置为分离状态后,线程结束时,它的资源会被系统自动的回收,而不再需要在其它线程中对其进行 pthread_join() 操作。
- 为什么线程分离(WHY)
在我们使用默认属性创建一个线程的时候,线程是 joinable 的。 joinable 状态的线程,必须在另一个线程中使用 pthread_join() 等待其结束,如果一个 joinable 的线程在结束后,没有使用 pthread_join() 进行操作,这个线程就会变成”僵尸线程”。每个僵尸线程都会消耗一些系统资源,当有太多的僵尸线程的时候,可能会导致创建线程失败。
换句话说
分离线程的函数叫做pthread_detach
pthread_detach函数的函数原型如下:
参数说明:
返回值说明:
例如,创建新线程后让新线程将自己进行分离,那么此后主线程就不需要在对新线程进行join了。
- #include <iostream>
- #include <pthread.h>
- #include <unistd.h>
- #include<string>
- using namespace std;
-
- pthread_t main_thread;
-
- void* Routine(void* arg)
- {
- pthread_detach(pthread_self());
-
- char* msg = (char*)arg;
- int cnt=0;
- while (1){
- printf("I am %s , %d\n", msg,getpid());
- sleep(1);
- cnt++;
- if(cnt==3)
- {
- break;
- }
-
- }
- printf("thread 1...quit\n");
-
- pthread_exit((void*)0);
- }
- int main()
- {
-
- pthread_t tid;
- pthread_create(&tid, NULL, Routine, (void*)"thread 1");
- sleep(5);
-
- cout<<"main thread quit ...."<<endl;
-
- return 0;
- }

这新线程在退出时,系统会自动回收对应线程的资源,不需要主线程进行join。
例如,下面我们创建五个新线程后让这五个新线程将自己进行分离,那么此后主线程就不需要在对这五个新线程进行join了。
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <sys/types.h>
-
-
- void* Routine(void* arg)
- {
- pthread_detach(pthread_self());
-
- char* msg = (char*)arg;
- int cnt=0;
- while (1){
- printf("I am %s , %d\n", msg,getpid());
- sleep(1);
- cnt++;
- if(cnt==3)
- {
- break;
- }
-
- }
- printf("thread 1...quit\n");
- delete msg;
-
- pthread_exit((void*)0);
- }
-
- int main()
- {
- pthread_t tid[5];
- for (int i = 0; i < 5; i++){
- char* buffer = (char*)malloc(64);
- sprintf(buffer, "thread %d", i);
- pthread_create(&tid[i], NULL, Routine, buffer);
- }
- while (1){
- printf("I am main thread...pid: %d, ppid: %d\n", getpid(), getppid());
- sleep(2);
- }
- return 0;
- }

我们可以监控一下
怎么样?很神奇吧!!!!
注意一点:
主线程也可以分离我们的线程,就通过那个pthread_creat获取线程的tid,然后就将他分离出来。
有没有想过整个pthread库里很多函数的参数都是void*,这样子有什么好处呢?
在多线程编程中,pthread库的函数参数设计为void*类型,这样做有多个好处。这种设计方式不仅体现了C语言的灵活性和对底层的控制,同时也为多线程编程提供了一定的便利和扩展性。以下是对这种设计的一些优点分析:
使用void*当参数类型,我们创建线程的时候,可以传更多的东西 ,千万不要认为只能传字符串啊!
目前我们创建多线程使用的是原生线程库,那么c++语言本身也支持多线程!!!C++的多线程和我们linux的多线程什么关系?
- #include <iostream>
- #include <thread>
- #include <unistd.h>
- #include<string>
- using namespace std;
-
- void threadrun()
- {
- while(1)
- {
- cout<<"I am a new thread for C++"<<endl;
- sleep(1);
- }
- }
- int main()
- {
- thread t1(threadrun);
-
- t1.join();
- }

呦呵,C++多线程的底层居然调用linux的pthread库的方法,C++多线程在linux环境下必须依靠pthread,不信?
我们可以接着看一下
其实不止C++语言,其他语言,在linux环境下都会调用线程库pthread
其实我们比较推荐语言级别的多线程代码,这个具有跨平台性!!不止在linux下可以运行,也可以在winodws下运行
还记得我们上面遗漏的那个问题吗?
- #include <iostream>
- #include <pthread.h>
- #include <unistd.h>
- #include<string>
- using namespace std;
-
-
- void* Routine(void* arg)
- {
- char* msg = (char*)arg;
- while (1){
- printf("I am %s , %d\n", msg,getpid());
- sleep(1);
- }
- return (void*)0;
- }
- int main()
- {
- pthread_t tid;
- pthread_create(&tid, NULL, Routine, (void*)"thread 1");
- while (1){
- printf("I am main thread : %d, new pthread tid:%p!\n",getpid(),tid);
- sleep(2);
-
- }
- return 0;
- }

打印出来的是这个玩意
这个和我预想中的怎么不一样,不应该是下面这种吗
我们在获取线程ID的时候发现和ps -aL的LWP结果不一样
pthread_create函数会产生一个线程ID,存放在第一个参数指向的地址中,该线程ID和内核中的LWP不是一回事。
显然tid
和LWP
是不一样的,用户能获取的线程ID不是内核中的LWP,
而内核中的LWP
其实也不需要给用户呈现,tid
是一个库内部自己维护的唯一值,因为库内部需要承担对线程的管理维护。
tid是什么?Lwp是什么?pthread_t到底是什么类型呢?
这个需要库的知识
首先,通过ldd命令可以看到,我们采用的线程库实际上是一个动态库。
库没被加载的时候是在磁盘中的,而线程库是一个动态库,本质上是一个文件。可执行程序也是在磁盘中。刚开始运行的时候,可执行程序会先变成一个进程,加载代码和数据到内存中,并同步创建PCB
,页表,地址空间…当CPU调度到这个进程,会运行代码,进而动态创建多线程!
接下来,运行到创建线程的代码的前提是把库加载到进程的地址空间中!动态库也要加载到内存中。进程如果要使用动态库的话,需要将动态库映射到进程地址空间的共享区中!代码运行到动态库中的代码时会跳转到共享区对应函数,在通过其偏移量映射到内存中库函数实现部分,完成动态库函数的调用!
进程运行时动态库被加载到内存,然后通过页表映射到进程地址空间中的共享区,此时该进程内的所有线程都是能看到这个动态库的。
好了,了解完库的知识就该上路了
LWP
(轻量级进程ID)维护线程是线程库维护的!接下来我们来看看线程库内部是如何维护管理的?
库管理线程和内核管理进程类似!同样遵循先描述,再组织:
我们说每个线程都有自己私有的栈,其中主线程采用的栈是进程地址空间中原生的栈,而其余线程采用的栈就是在共享区中开辟的。
我们可以验证一下每个进程都有自己的栈
#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h> #include <sys/types.h> void* Routine(void* arg) { char* msg = (char*)arg; int i=0; i++; while (1){ printf("I am %s...pid: %d, ppid: %d ,i :%d\n", msg, getpid(), getppid(),i); sleep(1); } delete msg; } int main() { pthread_t tid[5]; for (int i = 0; i < 5; i++){ char* buffer = (char*)malloc(64);//注意这里啊 sprintf(buffer, "thread %d", i); pthread_create(&tid[i], NULL, Routine, buffer); } while (1){ printf("I am main thread...pid: %d, ppid: %d\n", getpid(), getppid()); sleep(2); } return 0; }我们运行一下
我们发现每个进程的n都是从0开始的哦!这代表它们有自己独立的栈
好了,接着说
接下来我们来看看线程库内部是如何维护管理的?
上面我们所用的各种线程函数,本质都是在库内部对线程属性进行的各种操作,最后将要执行的代码交给对应的内核级LWP去执行就行了,也就是说线程数据的管理本质是在共享区的。
也就是在库中创建了描述线程的相关结构体字段属性,因为是连续开辟的,所以管理方法类似数组。 每一个新线程在共享区都有这样一块区域对其进行描述,因此我们要找到一个用户级线程只需要找到该线程内存块的起始地址,然后就可以获取到该线程的各种信息。而pthread_t tid就是该线程内存块的起始地址!通过这个地址我们就可以访问这个内存块的所有属性!
事实上,pthread_t 是 POSIX 线程库中的一个类型,用于唯一标识线程。这个标识符可能是一个简单的整数、指针或其他类型的结构体,取决于具体的实现。
- 我们对比一下FILE* open()这个接口会返回一个文件指针(而不是一个文件对象),那么这个指针指向的文件是在哪里呢?
在C标准库中!返回的也是一个地址(指针)。这和创建线程是一致的!
注意一点:在库里维护不一定要在库里(栈区)开辟空间,也可以在其他区域,比如通过malloc在堆区开辟空间!
- 再来看
pthread_join
,如何理解?因为在库内部中线程结束时会直接return,并没有进行资源的释放,所以如果不进行
join
就会产生内存泄漏!pthread_join就是通过tid来找到对应位置来释放资源!
一个线程内部就可以有一个数组来维护一个栈结构!线程就独立的拥有自己的栈结构了!栈空间本质是地址空间的一部分区域!主线程使用自己的栈,新线程使用自己开辟的栈!
在用户层面是线程,内核层是轻量级进程,他们是1 :1的。
Linux的线程 = pthread库中线程的属性集 + LWP
总的来说,pthread_t tid线程属性集合的起始虚拟地址 —— 在pthread中进行维护。
我们知道多个线程是共享同一个全局变量的
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <sys/types.h>
-
- int i=0;
-
- void* Routine(void* arg)
- {
- char* msg = (char*)arg;
- i++;
- while (1){
- printf("I am %s ,n :%d ,&n :%d\n",msg,i,&i);
- sleep(1);
- }
- delete msg;
- }
-
- int main()
- {
- pthread_t tid[3];
- for (int i = 0; i < 3; i++){
- char* buffer = (char*)malloc(64);
- sprintf(buffer, "thread %d", i);
- pthread_create(&tid[i], NULL, Routine, buffer);
- }
- while (1){
- printf("I am main thread...pid: %d, ppid: %d\n", getpid(), getppid());
- sleep(2);
- }
- return 0;
- }

那要是我们希望每个线程能有自己私有的全局变量多好。
其实很简单
__thread int i=0;//注意
完美啦,完美发现这个地址也不同了。
注意:__thread只能用于内置类型
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。