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 & ...
随机推荐
- Spring boot 添加日志 和 生成接口文档
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...
- [ios]sqlite轻量级数据库学习连接
SQLLite (一)基本介绍 http://blog.csdn.net/lyrebing/article/details/8224431 SQLLite (二) :sqlite3_open, sql ...
- 【转】cs231n学习笔记-CNN-目标检测、定位、分割
原文链接:http://blog.csdn.net/myarrow/article/details/51878004 1. 基本概念 1)CNN:Convolutional Neural Networ ...
- 算法笔记--KMP算法 && EXKMP算法
1.KMP算法 这个博客写的不错:http://www.cnblogs.com/SYCstudio/p/7194315.html 模板: next数组的求解,那个循环本质就是如果相同前后缀不能加上该位 ...
- Python 网络编程和Socket
2017-07-24 20:43:49 Socket又称"套接字",应用程序通常通过"套接字"向网络发出请求或者应答网络请求.Http协议主要的操作流程是req ...
- PHP访问Oracle数据库
说明:Oracle数据库帐号:sticOracle数据库密码:sticOracle数据库实例:orclOracle数据库表:UserInfoOracle表的列:ID,Name 不处理异常的代码如下:/ ...
- Rspec: everyday-rspec实操: 第8章DRY. (6个方法,其中3个方法好上手)
Don't Repeat Yourself. • 把操作步骤提取到辅助模块中;✅ • 通过let复用测试中的实例变量;✅ • 把通用的设置移到共享的情景中;⚠️(不喜欢) • 在RSpec和rspec ...
- mysql--------大数据量分页sql语句优化
分页程序原理很简单,这里就不多说了,本篇文章主要说的是在数据表记录量比较大的情况下,如何将分页SQL做到更优化,让MySQL执行的更快的方法. 一般的情况下,我们的分页SQL语句是这样的: ,; 以上 ...
- 『Scrapy』终端调用&选择器方法
Scrapy终端 示例,输入如下命令后shell会进入Python(或IPython)交互式界面: scrapy shell "http://www.itcast.cn/channel/te ...
- MQTT协议QoS服务质量 (Quality of Service 0, 1 & 2)概念学习
什么是 QoS ? QoS (Quality of Service) 是发送者和接收者之间,对于消息传递的可靠程度的协商. QoS 的设计是 MQTT 协议里的重点.作为专为物联网场景设计的协议,MQ ...