Linux系统编程——多线程实现多任务
概述
每一个进程都拥有自己的数据段、代码段和堆栈段。这就造成进程在进行创建、切换、撤销操作时,须要较大的系统开销。
为了降低系统开销,从进程中演化出了线程。为了让进程完毕一定的工作。进程必须至少包括一个线程。线程存在于进程中,共享进程的资源。
很多其它详情。请看《进程和线程的差别与联系》。
就像每一个进程都有一个进程号一样。每一个线程也有一个线程号。
进程号在整个系统中是唯一的,但线程号不同,线程号仅仅在它所属的进程环境中有效。进程号用 pid_t 数据类型表示。是一个非负整数。线程号则用 pthread_t 数据类型来表示,Linux 使用无符号长整数表示。有的系统在实现 pthread_t 的时候,用一个结构体来表示,所以在可移植的操作系统实现不能把它做为整数处理。
线程的经常使用函数
1)获取线程号
所需头文件:
#include <pthread.h>
pthread_t pthread_self(void);
功能:
获取线程号。
參数:
无
返回值:
调用线程的线程 ID 。
2)线程号的比較
所需头文件:
#include <pthread.h>
int pthread_equal(pthread_t t1, pthread_t t2);
功能:
推断线程号 t1 和 t2 是否相等。
为了方便移植。尽量使用函数来比較线程 ID。
參数:
t1。t2:待推断的线程号。
返回值:
相等: 非 0
不相等:0
演示样例代码:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h> int main(int argc, char *argv[])
{
pthread_t thread_id; thread_id = pthread_self(); // 返回调用线程的线程ID
printf("Thread ID = %lu \n",thread_id); if( 0 != pthread_equal( thread_id, pthread_self() ) ){
printf("Equal!\n");
}else{
printf("Not equal!\n");
} return 0;
}
线程函数的程序在 pthread 库中。故链接时要加上參数 -lpthread。
执行结果例如以下:
3)线程的创建
所需头文件:
#include <pthread.h>
int pthread_create( pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine)(void *),
void *arg );
功能:
创建一个线程。
參数:
thread:线程标识符地址。
attr:线程属性结构体地址。通常设置为 NULL。
start_routine:线程函数的入口地址。
arg:传给线程函数的參数。
返回值:
成功:0
失败:非 0
pthread_create() 创建的线程从指定的回调函数開始执行,该函数执行完后,该线程也就退出了。线程依赖进程存在的。共享进程的资源,假设创建线程的进程结束了。线程也就结束了。
演示样例一:
#include <stdio.h>
#include <unistd.h>
#include <pthread.h> int var = 8; void *thread_1(void *arg)
{
while(1)
{
printf("this is my new thread1: var++\n");
var++;
sleep(1);
}
return NULL;
} void *thread_2(void * arg)
{
while(1){
printf("this is my new thread2: var = %d\n", var);
sleep(1);
} return NULL;
} int main(int argc, char *argv[])
{
pthread_t tid1,tid2; //创建两个线程
pthread_create(&tid1, NULL, thread_1, NULL);
pthread_create(&tid2, NULL, thread_2, NULL); while(1){
printf("the main thread: var = %d\n", var);
sleep(1);
} return 0;
}
执行结果例如以下:
演示样例二:
#include <stdio.h>
#include <unistd.h>
#include <pthread.h> // 回调函数
void *thread_fun(void * arg)
{
sleep(1);
int num = *( (int *)arg );
printf("int the new thread: num = %d\n", num); return NULL;
} int main(int argc, char *argv[])
{
pthread_t tid;
int test = 100; // 创建线程, 把 &test 传给回调函数 thread_fun()
pthread_create(&tid, NULL, thread_fun, (void *)&test); while(1); return 0;
}
执行结果例如以下:
4)回收线程资源
所需头文件:
#include <pthread.h>
int pthread_join(pthread_t thread, void **retval);
功能:
等待线程结束(此函数会堵塞),并回收线程资源,类似进程的 wait() 函数。假设线程已经结束。那么该函数会马上返回。
參数:
thread:被等待的线程号。
retval:用来存储线程退出状态的指针的地址。
返回值:
成功:0
失败:非 0
演示样例代码例如以下:
#include <stdio.h>
#include <unistd.h>
#include <pthread.h> void *thead(void *arg)
{
static int num = 123; //静态变量 printf("after 2 seceonds, thread will return\n");
sleep(2); return #
} int main(int argc, char *argv[])
{
pthread_t tid;
int ret = 0;
void *value = NULL; // 创建线程
ret = pthread_create(&tid, NULL, thead, NULL);
if(ret != 0){ //创建失败
perror("pthread_create");
} // 等待线程号为 tid 的线程。假设此线程结束就回收其资源
// &value保存线程退出的返回值
pthread_join(tid, &value); printf("value = %d\n", *( (int *)value ) ); return 0;
}
执行结果例如以下:
创建一个线程后应回收其资源,但使用 pthread_join() 函数会使调用者堵塞,Linux 还提供了非堵塞函数 pthread_detach() 来回收线程的资源。
所需头文件:
#include <pthread.h>
int pthread_detach(pthread_t thread);
功能:
使调用线程与当前进程分离。分离后不代表此线程不依赖与当前进程,线程分离的目的是将线程资源的回收工作交由系统自己主动来完毕。也就是说当被分离的线程结束之后,系统会自己主动回收它的资源。所以,此函数不会堵塞。
參数:
thread:线程号。
返回值:
成功:0
失败:非 0
注意,调用 pthread_detach() 后再调用 pthread_join() , pthread_join() 会立刻返回,调用失败。
演示样例代码例如以下:
#include <stdio.h>
#include <unistd.h>
#include <pthread.h> void *thead(void *arg)
{
int i;
for(i=0; i<5; i++)
{
printf("I am runing\n");
sleep(1);
} return NULL;
} int main(int argc, char *argv[])
{
int ret = 0;
pthread_t tid; ret = pthread_create(&tid, NULL, thead, NULL);
if(ret!=0){
perror("pthread_create");
} pthread_detach(tid); // 线程分离。不堵塞 // 立刻返回。调用失败
int flag = pthread_join(tid, NULL);
if(flag != 0){
printf("join not working\n");
} printf("after join\n");
sleep(3);
printf("I am leaving\n"); return 0;
}
执行结果例如以下:
5)线程退出
在进程中我们能够调用 exit() 函数或 _exit() 函数来结束进程。在一个线程中我们能够通过 pthread_exit() 在不终止整个进程的情况下停止它的控制流。
所需头文件:
#include <pthread.h>
void pthread_exit(void *retval);
功能:
退出调用线程。一个进程中的多个线程是共享该进程的数据段。因此,通常线程退出后所占用的资源并不会释放。
參数:
retval:存储线程退出状态的指针。
返回值:
无
演示样例代码例如以下:
#include <stdio.h>
#include <unistd.h>
#include <pthread.h> void *thread(void *arg)
{
static int num = 123; //静态变量
int i = 0;
while(1)
{
printf("I am runing\n");
sleep(1);
i++;
if(i==3)
{
pthread_exit( (void *)&num );
// return #
}
} return NULL;
} int main(int argc, char *argv[])
{
int ret = 0;
pthread_t tid;
void *value = NULL; ret = pthread_create(&tid, NULL, thread, NULL);
if(ret!=0){
perror("pthread_create");
} pthread_join(tid, &value); printf("value = %d\n", *(int *)value ); return 0;
}
执行结果例如以下:
Linux系统编程——多线程实现多任务的更多相关文章
- Linux系统编程@多线程编程(二)
线程的操作 线程标识 线程的ID表示数据类型:pthread_t (内核中的实现是unsigned long/unsigned int/指向pthread结构的指针(不可移植)几种类型) 1.对两个线 ...
- Linux系统编程@多线程编程(一)
多线程编程 涉及操作系统原理概念 时间片 进程状态 上下文: 对进程来说,就是进程的执行环境,具体就是各个变量和数据,包括所有的寄存器变量.打开的文件.内存信息等. 进程的写时复制:由于一般 fork ...
- Linux系统编程@多线程与多进程GDB调试
博客内容参考自 http://www.cnblogs.com/xuxm2007/archive/2011/04/01/2002162.html http://blog.csdn.net/pbymw8i ...
- Linux系统编程——线程私有数据
在多线程程序中.常常要用全局变量来实现多个函数间的数据共享.因为数据空间是共享的,因此全局变量也为全部线程共同拥有. 測试代码例如以下: #include <stdio.h> #inclu ...
- Linux 系统编程 学习:11-线程:线程同步
Linux 系统编程 学习:11-线程:线程同步 背景 上一讲 我们介绍了线程的属性 有关设置.这一讲我们来看线程之间是如何同步的. 额外安装有关的man手册: sudo apt-get instal ...
- Linux系统编程温故知新系列 --- 01
1.大端法与小端法 大端法:按照从最高有效字节到最低有效字节的顺序存储,称为大端法 小端法:按照从最低有效字节到最高有效字节的顺序存储,称为小端法 网际协议使用大端字节序来传送TCP分节中的多字节整数 ...
- Linux 系统编程
简介和主要概念 Linux 系统编程最突出的特点是要求系统程序员对它们工作的的系统的硬件和操作系统有深入和全面的了解,当然它们还有库和系统调用上的区别. 系统编程分为:驱动编程.用户空间编程和网络编程 ...
- 《Linux系统编程(第2版)》
<Linux系统编程(第2版)> 基本信息 作者: (美)Robert Love 译者: 祝洪凯 李妹芳 付途 出版社:人民邮电出版社 ISBN:9787115346353 上架时间:20 ...
- Linux 系统编程 学习:05-进程间通信2:System V IPC(2)
Linux 系统编程 学习:05-进程间通信2:System V IPC(2) 背景 上一讲 进程间通信:System V IPC(1)中,我们介绍了System IPC中有关消息队列.共享内存的概念 ...
随机推荐
- 分布式系统CAP原则与BASE思想
一.CAP原则 CAP原则又称CAP定理,指的是在一个分布式系统中, Consistency(一致性). Availability(可用性).Partition tolerance(分区容错性),三者 ...
- JavaScript:对象
ylbtech-JavaScript:对象 1. JavaScript Array 对象返回顶部 1. JavaScript Array 对象 Array 对象 Array 对象用于在变量中存储多个值 ...
- k8s traefik ingress tls
使用下面的 openssl 命令生成 CA 证书: $ openssl req -newkey rsa:2048 -nodes -keyout tls.key -x509 -days 365 -out ...
- Asura监控---AsuraMonitor,阿修罗监控开源
阿修罗Monitor是一个功能强大.灵活的监控系统. 系统安装简单,配置简单,相比zabbix, nagios,cacti,小米监控等都使用相当简单.只需要会写脚本,语言不限就可以实现任意监控需求. ...
- php对文件/目录操作的基础知识(图解)
具体的如下图所示:
- javascript中的构造函数和原型及原型链
纯属个人理解,有错误的地方希望大牛指出,以免误人子弟 1.构造函数: 构造函数的作用 : 初始化由new创建出来的对象 new 的作用: 创建对象(空对象) new 后面跟的是函数调用,使用ne ...
- mybatis 高级映射和spring整合之逆向工程(7)
mybatis 高级映射和spring整合之逆向工程(7) 4.0 逆向工程 4.1 mybatis需要程序员自己编写sql语句,mybatis官方提供逆向工程,可以针对单表自动生成mybatis执行 ...
- map参数值取代
public static String processTemplate(String tpl, Map<String, ?> params){ Iterator<String> ...
- 我的新书《计算机图形学基础(OpenGL版)》
我的新书<计算机图形学基础(OpenGL版)>今年6月份在清华大学出版社出版了!新书与原在机械工业出版社出的<计算机图形学>相比,主要有以下不同: 1.加重OpenGL的内容, ...
- JS面向对象(2)——原型链
原型链用于ECMAScript的继承.其思想是利用原型让一个引用类型继承另一个引用类型的属性和方法.说人话,我们知道,一个构造函数Subtype,其原型对象有一个指向构造函数的指针,这是联系构造函数和 ...