概述

每一个进程都拥有自己的数据段、代码段和堆栈段。这就造成进程在进行创建、切换、撤销操作时,须要较大的系统开销。

为了降低系统开销,从进程中演化出了线程。为了让进程完毕一定的工作。进程必须至少包括一个线程。线程存在于进程中,共享进程的资源。

很多其它详情。请看《进程和线程的差别与联系》。

就像每一个进程都有一个进程号一样。每一个线程也有一个线程号。

进程号在整个系统中是唯一的,但线程号不同,线程号仅仅在它所属的进程环境中有效。进程号用 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 &num;
}
} 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系统编程——多线程实现多任务的更多相关文章

  1. Linux系统编程@多线程编程(二)

    线程的操作 线程标识 线程的ID表示数据类型:pthread_t (内核中的实现是unsigned long/unsigned int/指向pthread结构的指针(不可移植)几种类型) 1.对两个线 ...

  2. Linux系统编程@多线程编程(一)

    多线程编程 涉及操作系统原理概念 时间片 进程状态 上下文: 对进程来说,就是进程的执行环境,具体就是各个变量和数据,包括所有的寄存器变量.打开的文件.内存信息等. 进程的写时复制:由于一般 fork ...

  3. Linux系统编程@多线程与多进程GDB调试

    博客内容参考自 http://www.cnblogs.com/xuxm2007/archive/2011/04/01/2002162.html http://blog.csdn.net/pbymw8i ...

  4. Linux系统编程——线程私有数据

    在多线程程序中.常常要用全局变量来实现多个函数间的数据共享.因为数据空间是共享的,因此全局变量也为全部线程共同拥有. 測试代码例如以下: #include <stdio.h> #inclu ...

  5. Linux 系统编程 学习:11-线程:线程同步

    Linux 系统编程 学习:11-线程:线程同步 背景 上一讲 我们介绍了线程的属性 有关设置.这一讲我们来看线程之间是如何同步的. 额外安装有关的man手册: sudo apt-get instal ...

  6. Linux系统编程温故知新系列 --- 01

    1.大端法与小端法 大端法:按照从最高有效字节到最低有效字节的顺序存储,称为大端法 小端法:按照从最低有效字节到最高有效字节的顺序存储,称为小端法 网际协议使用大端字节序来传送TCP分节中的多字节整数 ...

  7. Linux 系统编程

    简介和主要概念 Linux 系统编程最突出的特点是要求系统程序员对它们工作的的系统的硬件和操作系统有深入和全面的了解,当然它们还有库和系统调用上的区别. 系统编程分为:驱动编程.用户空间编程和网络编程 ...

  8. 《Linux系统编程(第2版)》

    <Linux系统编程(第2版)> 基本信息 作者: (美)Robert Love 译者: 祝洪凯 李妹芳 付途 出版社:人民邮电出版社 ISBN:9787115346353 上架时间:20 ...

  9. Linux 系统编程 学习:05-进程间通信2:System V IPC(2)

    Linux 系统编程 学习:05-进程间通信2:System V IPC(2) 背景 上一讲 进程间通信:System V IPC(1)中,我们介绍了System IPC中有关消息队列.共享内存的概念 ...

随机推荐

  1. B1734 [Usaco2005 feb]Aggressive cows 愤怒的牛 二分答案

    水题,20分钟AC,最大值最小,一看就是二分答案... 代码: Description Farmer John has built a <= N <= ,) stalls. The sta ...

  2. 第8章 MyBatis简介

    # 创建一个名称为mybatis的数据库 CREATE DATABASE mybatis; # 使用名称为mybatis的数据库 USE mybatis; # 创建一个tb_user表,有id.nam ...

  3. linux git保存用户名密码(避免每次push输用户名密码)

    Linux/Unix/Mac 系统 新建一个 ~/.netrc 文件, 将 git 服务器, 用户名以及密码记录在这个文件, 如下所示:   machine your-git-server   log ...

  4. Wannafly挑战赛25 C 期望操作数 数学

    题目 题意:给你你一个数x和一个数q,x<=q,每一次可以等概率把x变成[x,q]中任意一个数,问变成q的步数的期望,输出对998244353取模,多组询问 题解:首先肯定的是,可以预处理,因为 ...

  5. anaconda 使用 及 tensorflow-gpu 安装

    Anaconda简易使用 创建新环境 conda create -n rcnn python=3.6 删除环境 conda remove -n rcnn --all 进入环境 conda activa ...

  6. 使用数组实现ArrayList的效果

    package day04.d2.shuzu; /** * 通过数组实现类似于集合的功能 * 包含功能有: * * 动态添加元素 * 在指定位置添加元素 * * 删除指定下标的元素 * 删除指定内容的 ...

  7. 使用asp.net 开发的一个东平人才网招聘程序

    本人用asp.net 工作已经一两年时间了,在单位一般是做管理系统类的开发,针对的客户大都是政府机关,所以都是内网系统,虽然有成就感,但是无法再互联网上展示.虽然技术一般,但还是希望自己做一个网站在互 ...

  8. 关于KO信息

    最近写大论文查到KO也是可以用于分类的一种信息. 如何使用KEGG进行通路富集http://blog.sciencenet.cn/blog-364884-779116.html kegg 数据库学习笔 ...

  9. 第八周读书笔记(人月神话X月亮与六便士)——到底什么才是一个程序员的自我修养?

    写了这么久的读书笔记,涉及到问题大多是一些如何把软件工程做好,如何把自己的职业生涯做好.但总感觉逻辑链上缺了一环,亦即:我们为什么要把软件工程做好,我们成为一名优秀的职业生涯的意义到底在于什么?我觉得 ...

  10. jquey中的事件绑定

    三种方法: $(selector).live(events, data, handler);                // jQuery 1.3+ $(document).delegate(se ...