12.2 linux下的线程
什么是线程:
在一个程序里的一个执行路线就叫做线程(thread),更准确的定义是:线程是“一个进程内部的控制序列”
一切进程至少都有一个执行线程
进程与线程:
进程是资源竞争的基本单位
线程是程序执行的最小单位
线程会使用进程的全局变量
线程共享进程数据,但也拥有自己的一部分数据
线程ID
程序计数器
寄存器组
栈
errno
一个进程内部的线程可以共享资源
代码段
数据段
打开文件和信号
单线程和多线程模型如下:

如何从进程往线程中传数据?又如何从线程中将数据传出来呢?
1、使用全局变量,可以将数据传给线程
2、在进程中分配内存,通过pthread_create的第四个参数传给线程
fork和创建新线程的区别:



线程的属性是可以修改的。竞争范围可以修改。
POSIX线程库:
与线程有关的函数构成了一个完整的系列,绝大多数函数名字都是以“pthread”开头的。
要使用这些函数库,需要引入头文件<pthread.h>
链接这些线程函数库时要使用编译器命令的“-lpthread”选项
pthread_create函数:
原型:int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
功能:创建一个新的线程
参数:
thread:返回的线程ID
attr:设置线程的属性,attr为NULL表示使用默认属性
start_routine:是一个函数地址,线程启动后要执行的函数
arg:传给线程启动函数的参数
返回值:成功返回0,失败返回错误码
错误检查:
传统的一些函数是成功返回0,失败返回-1,并且对全局变量errno赋值以指示错误
pthreads函数出错时不会设置全局变量errno(而大部分其他POSIX函数会这样做),而是将错误码通过返回值返回
pthread同样也提供了线程内的errno变量,以支持其他使用errno的代码,对于pthreads函数的错误,建议通过返回值来判定,因为读取返回值
要比读取线程内的errno变量的开销更小。
其他线程函数:



进程和线程对比:
进程:
fork;有pid;有pcb控制块;有僵尸进程;
线程:
pthread_create;有tid;有tcb控制块;有僵尸线程
线程和进程有一个巨大的区别,就是线程依赖进程,如果进程死了,线程也会死掉。而进程是父进程死了,子进程依旧可以运行。因为进程有自己独立的内存空间。总之,线程依赖于进程的生命周期。
线程示例程序如下:
#include <unistd.h>
#include <sys/types.h> #include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
/*
int pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void*), void *restrict arg);
*/ void *start_routine(void *arg)
{
int i = ;
printf("I am thread. \n"); for(i = ; i < ; i++)
printf("B");
fflush(stdout); return NULL;
} int main()
{
pthread_t thread;
int i = ; pthread_create(&thread, NULL, start_routine, NULL); for(i = ; i< ; i++)
{
printf("A");
fflush(stdout);
} return ;
}
执行完32行,线程就去执行线程体函数了,34行开始的for循环已经不再属于线程,也就是说线程执行完线程体就返回了,不会执行到34行。
运行结果如下:

我们看到只打印出了AAAAAAAAA,这是因为,进程(主线程)死了之后,线程也就死了,它还没有来得及执行线程体就死了。我们给进程下面加上sleep,如下:
#include <unistd.h>
#include <sys/types.h> #include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
/*
int pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void*), void *restrict arg);
*/ void *start_routine(void *arg)
{
int i = ;
printf("I am thread. \n"); for(i = ; i < ; i++)
printf("B");
fflush(stdout); return NULL;
} int main()
{
pthread_t thread;
int i = ; pthread_create(&thread, NULL, start_routine, NULL); for(i = ; i< ; i++)
{
printf("A");
fflush(stdout);
} sleep(); return ;
}
结果如下:

我们看到线程成功打印出了数据。
打印全局变量,程序如下:
#include <unistd.h>
#include <sys/types.h> #include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
/*
int pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void*), void *restrict arg);
*/ int g_num = ; void *start_routine(void *arg)
{
int i = ;
printf("I am thread. \n");
printf("g_num = %d\n", g_num); for(i = ; i < ; i++)
{
printf("B");
fflush(stdout);
} printf("\n"); return NULL;
} int main()
{
pthread_t thread;
int i = ; g_num = ; pthread_create(&thread, NULL, start_routine, NULL); for(i = ; i< ; i++)
{
printf("A");
fflush(stdout);
}
printf("\n");
sleep(); return ;
}
执行结果如下:

多进程打印全局变量程序如下:
#include <unistd.h>
#include <sys/types.h> #include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
/*
int pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void*), void *restrict arg);
*/ int g_num = ; void *start_routine(void *arg)
{
int i = ;
printf("I am thread. \n");
printf("g_num = %d\n", g_num); for(i = ; i < ; i++)
{
printf("B");
fflush(stdout);
} printf("\n"); return NULL;
} int main()
{
pid_t pid;
int i = ; g_num = ; pid = fork(); if( pid == )
{
printf("I am child \n");
printf("g_num = %d\n", g_num);
for(i = ; i< ; i++)
{
printf("B");
fflush(stdout);
}
printf("\n");
exit();
} for(i = ; i< ; i++)
{
printf("A");
fflush(stdout);
}
printf("\n");
sleep(); return ;
}
结果如下:

获取线程ID的示例:
#include <unistd.h>
#include <sys/types.h> #include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
/*
int pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void*), void *restrict arg);
*/ int g_num = ; void *start_routine(void *arg)
{
int i = ;
printf("I am thread. \n");
printf("g_num = %d\n", g_num);
printf("thread id = %lu\n", pthread_self()); for(i = ; i < ; i++)
{
printf("B");
fflush(stdout);
} printf("\n"); return NULL;
} int main()
{
pthread_t thread;
int i = ; g_num = ; pthread_create(&thread, NULL, start_routine, NULL); for(i = ; i< ; i++)
{
printf("A");
fflush(stdout);
}
printf("\n");
sleep(); return ;
}
结果如下:

上面的程序中,我们使用sleep等待,让子进程先运行,但是这不是根本的解决办法,我们使用pthread_join等待子线程退出,程序如下:
#include <unistd.h>
#include <sys/types.h> #include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
/*
int pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void*), void *restrict arg);
*/ int g_num = ; void *start_routine(void *arg)
{
int i = ;
printf("I am thread. \n");
printf("g_num = %d\n", g_num);
printf("thread id = %lu\n", pthread_self()); for(i = ; i < ; i++)
{
printf("B");
fflush(stdout);
} sleep(); printf("\n"); return NULL;
} int main()
{
pthread_t thread;
int i = ; g_num = ; pthread_create(&thread, NULL, start_routine, NULL); for(i = ; i< ; i++)
{
printf("A");
fflush(stdout);
}
printf("\n"); pthread_join(thread, NULL); return ;
}
结果如下:

线程有两种死掉的方法,一个是自杀一个是他杀。
我们先用exit使子线程退出,程序如下:
#include <unistd.h>
#include <sys/types.h> #include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
/*
int pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void*), void *restrict arg);
*/ int g_num = ; void *start_routine(void *arg)
{
int i = ;
printf("I am thread. \n");
printf("g_num = %d\n", g_num);
printf("thread id = %lu\n", pthread_self()); for(i = ; i < ; i++)
{
printf("B");
fflush(stdout);
} sleep(); printf("\n"); exit();
} int main()
{
pthread_t thread;
int i = ; g_num = ; pthread_create(&thread, NULL, start_routine, NULL); for(i = ; i< ; i++)
{
printf("A");
fflush(stdout);
}
printf("\n"); pthread_join(thread, NULL);
printf("child thread die\n"); return ;
}
结果如下:

我们发现子线程退出后,父进程没有打印出第54行的数据,说明子线程用exit退出后,父进程也挂了,相当于同归于尽。因此,线程退出千万不能用exit。
我们可以使用pthread_exit让线程死掉,程序如下:
#include <unistd.h>
#include <sys/types.h> #include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
/*
int pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void*), void *restrict arg);
*/ int g_num = ; void *start_routine(void *arg)
{
int i = ;
printf("I am thread. \n");
printf("g_num = %d\n", g_num);
printf("thread id = %lu\n", pthread_self()); for(i = ; i < ; i++)
{
printf("B");
fflush(stdout);
} sleep(); printf("\n"); pthread_exit(NULL);
} int main()
{
pthread_t thread;
int i = ; g_num = ; pthread_create(&thread, NULL, start_routine, NULL); for(i = ; i< ; i++)
{
printf("A");
fflush(stdout);
}
printf("\n"); pthread_join(thread, NULL);
printf("child thread die\n"); return ;
}
执行结果如下:

可以看到第54行打印出来了。
线程他杀就是父进程调用pthread_cancel杀掉子线程,这个很少用。
父进程如果不想等待线程结束,可以让线程脱离这个进程运行,使用pthread_detach函数,程序如下:
#include <unistd.h>
#include <sys/types.h> #include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
/*
int pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void*), void *restrict arg);
*/ int g_num = ; void *start_routine(void *arg)
{
int i = ;
pthread_detach(pthread_self());
printf("I am thread. \n");
printf("g_num = %d\n", g_num);
printf("thread id = %lu\n", pthread_self()); for(i = ; i < ; i++)
{
printf("B");
fflush(stdout);
} sleep(); printf("\n"); pthread_exit(NULL);
} int main()
{
pthread_t thread;
int i = ; g_num = ; pthread_create(&thread, NULL, start_routine, NULL); for(i = ; i< ; i++)
{
printf("A");
fflush(stdout);
}
printf("\n"); pthread_join(thread, NULL);
printf("child thread die\n"); return ;
}
结果如下:

一般在线程体函数中立即调用pthread_detach函数。
我们编译多线程程序时要加上 -lpthread,使用ldd可以查看程序用了哪些动态库,如下所示:

可以使用nm查看一个库中的函数(符号),如下:

12.2 linux下的线程的更多相关文章
- Linux下查看线程数的几种方法汇总
Linux下查看线程数的几种方法汇总 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Linux下查看某个进程的线程数量 pstree命令以树状图显示进程间的关系(display ...
- Linux下Java线程具体监控和其dump的分析使用----分析Java性能瓶颈[张振华-Jack]
作者:张振华(Jack) 这里对linux下.sun(oracle) JDK的线程资源占用问题的查找步骤做一个小结: linux环境下,当发现java进程占用CPU资源非常高,且又要想更进一步查出哪一 ...
- Linux下进程线程,Nignx与php-fpm的进程线程方式
1.进程与线程区别 进程是程序执行时的一个实例,即它是程序已经执行到课中程度的数据结构的汇集.从内核的观点看,进程的目的就是担当分配系统资源(CPU时间.内存等)的基本单位. 线程是进程的一个执行流, ...
- Linux下简单线程池的实现
大多数的网络服务器,包括Web服务器都具有一个特点,就是单位时间内必须处理数目巨大的连接请求,但是处理时间却是比较短的.在传统的多线程服务器模型中是这样实现的:一旦有个服务请求到达,就创建一个新的服务 ...
- 一个Linux下C线程池的实现
什么时候需要创建线程池呢?简单的说,如果一个应用需要频繁的创建和销毁线程,而任务执行的时间又非常短,这样线程创建和销毁的带来的开销就不容忽视,这时也是线程池该出场的机会了.如果线程创建和销毁时间相比任 ...
- linux下的线程池
什么时候需要创建线程池呢?简单的说,如果一个应用需要频繁的创建和销毁线程,而任务执行的时间又非常短,这样线程创建和销毁的带来的开销就不容忽视,这时也是线程池该出场的机会了.如果线程创建和销毁时间相比任 ...
- linux下使用线程锁互斥访问资源
linux使用线程锁访问互斥资源: 1.线程锁的创建 pthread_mutex_t g_Mutex; 2.完整代码如下 #include <stdio.h> #include <s ...
- Linux下的线程
一.线程的优点 与传统进程相比,用线程来实现相同的功能有如下优点: (1)系统资源消耗低. (2)速度快. (3)线程间的数据共享比进程间容易的多. 二.多线程编程简单实例 #include < ...
- Linux下获取线程TID的方法——gettid()
(转载)http://blog.csdn.net/delphiwcdj/article/details/8476547 如何获取进程的PID(process ID)? 可以使用: #include & ...
随机推荐
- python 时间元组转可视化时间(自定义)
>>> time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) '2019-01-04 11:20:25'
- [Java学习] Java多态和动态绑定
在Java中,父类的变量可以引用父类的实例,也可以引用子类的实例. 请读者先看一段代码: 1. public class Demo { 2. public static void main(Strin ...
- 关于C和C++
最开始学的就是C和C++,但只是学过,根本就不知道怎么使用. 后来接触了Python和Perl才知道怎么将编程应用于实际需求当中,读取文件,存放到数据结构,处理,输出. 但脚本语言有其固有的缺点,不能 ...
- 解决jenkins运行selenium测试出错的问题
If you run Jenkins as a service in the background it won't open apps in the foreground. You may eith ...
- Jzzhu and Apples CodeForces - 449C (构造,数学)
大意: 求从[1,n]范围选择尽量多的数对, 使得每对数的gcd>1 考虑所有除2以外且不超过n/2的素数p, 若p倍数可以选择的有偶数个, 直接全部划分即可 有奇数个的话, 余下一个2*p不划 ...
- Connecting Vertices CodeForces - 888F (图论,计数)
链接 大意: 给定邻接表表示两点是否可以连接, 要求将图连成树, 且边不相交的方案数 n范围比较小, 可以直接区间dp $f[l][r]$表示答案, $g[l][r]$表示区间[l,r]全部连通且l, ...
- hdu6405Make ZYB Happy 广义sam
题意:给出n(n<=10000)个字符串S[1~n],每个S[i]有权值val[i],随机等概率造一个由小写字母构成的字符串T,Sum = 所有含有子串T的S[i]的val[i]之积,求Sum的 ...
- python-day16--内置函数
内置函数操作 #!usr/bin/env python # -*- coding:utf-8 -*- # 1.locals()和globals() # def func(): # x=1 # y=2 ...
- Excel 版本对应
(1) 1985年:Excel 1.0 (2) 1993年:Excel 5.0——Office 4.2 (3) 1995年:Excel 7.0(Excel 95)——Office 95 (4) 199 ...
- IDEA2017安装actibpmn插件中文乱码问题解决
1.修改idea安装目录下的两个文件 C:\Program Files\JetBrains\IntelliJ IDEA 2017.1.4\bin\idea.exe.vmoptions C:\Progr ...