Linux多线程编程基础入门(一)线程的基本概念

线程是操作系统能够进行调度运算的最小单位,它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务。

linux 操作系统使用符合 POSIX 线程作为系统标准线程,该 POSIX 线程标准定义了一整套操作线程的 API。

线程的创建

一个线程的生命周期起始于它被创建的那一刻,创建线程的接口:

#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);

函数说明:

POSIX thread 简称 pthread;pthread_t 是 unsigned long int 类型的变量,用来表示线程的 ID

函数参数:

thread(输出参数),由 pthread_create 在线程创建成功后返回的线程句柄,该句柄在后续操作线程的 API 中用于标志该新建的线程;
start_routine(输入参数),新建线程的入口函数;
arg(输入参数),传递给新线程入口函数的参数;
attr(输入参数),指定新建线程的属性,如线程栈大小等;如果值为 NULL,表示使用系统默认属性。
函数返回值:

成功,返回 0;失败,返回相关错误码。

需要注意:

  • 主线程,这是一个进程的初始线程,其入口函数为 main 函数。
  • 新线程的运行时机,一个线程被创建之后有可能不会被马上执行,甚至,在创建它的线程结束后还没被执行;也有可能新线程在当前线程从pthread_create前就已经在运行,甚至,在pthread_create前从当前线程返回前新线程就已经执行完毕。

线程ID

在新线程被创建后,便有了一个在其所在进程内(线程依附于进程而存在)唯一的标识符,由 pthread_t 表示,称为线程 ID。一个线程可以调用以下接口获取其 ID:

include <pthread.h>
pthread_t pthread_self(void);

pthread_self 直接返回调用线程的 ID。
判断两个线程 ID 的大小是没有任何意义的,但有时可能需要判断两个给定的线程 ID 是否相等,使用以下接口:

include <pthread.h>
int pthread_equal(pthread_t t1, pthread_t t2);

pthread_equal 如果 t1 和 t2 所指定的线程 ID 相同,返回非 0 值;否则返回 0。

从系统实现的角度观察线程的创建

创建一个新的线程,从系统实现的角度看,就是创建了一个新的可调度实体;同一个进程内的线程,共享绝大部分进程的资源,只有少部分信息是线程所特有的,如栈和线程特有数据等。下图(图片来源于《Linux/UNIX 系统编程手册》)是假设一个进程内存在 4 个线程时,内存资源的分配情况:
在这里插入图片描述
可以看出,同一进程内的线程间除了栈是特有的,其他内存资源几乎都是共享的。共享意味着,多个线程可以同时修改某一内存区,且该修改对同一进程的所有线程都是可见的。

线程的终止

一个线程的终止分两种形式:被动终止和主动终止

被动终止有两种方式:

  1. 线程所在进程终止,任意线程执行exit函数,都会导致进程终止,从而导致依附于该进程的所有线程终止。
  2. 其他线程调用pthread_cancel请求取消该线程。

主动终止也有两种方式:

  1. 在线程的入口函数中执行 return 语句,main 函数(主线程入口函数)执行 return 语句会导致进程终止,从而导致依附于该进程的所有线程终止。
  2. 线程调用 pthread_exit 函数,main 函数(主线程入口函数)调用 pthread_exit 函数,主线程终止,但如果该进程内还有其他线程存在,进程会继续存在,进程内其他线程继续运行。

线程终止函数:

include <pthread.h>
void pthread_exit(void *retval);
  • 线程调用 pthread_exit 函数会导致该调用线程终止,并且返回由 retval 指定的内容(如何获取返回值后面介绍)。
  • 注意: retval 不能指向该线程的栈空间,否则可能成为野指针!

管理线程的终止

线程的连接
一个线程的终止对于另外一个线程而言是一种异步的事件,有时我们想等待某个 ID 的线程终止了再去执行某些操作,pthread_join 函数为我们提供了这种功能,该功能称为线程的连接:

include <pthread.h>
int pthread_join(pthread_t thread, void **retval);

参数说明:

thread(输入参数),指定我们希望等待的线程
retval(输出参数),我们等待的线程终止时的返回值,就是在线程入口函数中 return 的值或者调用 pthread_exit 函数的参数
返回值:

  • 成功时,返回 0
  • 错误时,返回正数错误码

当线程 X 连接线程 Y 时,如果线程 Y 仍在运行,则线程 X 会阻塞直到线程 Y 终止;如果线程 Y 在被连接之前已经终止了,那么线程 X 的连接调用会立即返回。

连接线程其实还有另外一层意义,一个线程终止后,如果没有人对它进行连接,那么该终止线程占用的资源,系统将无法回收,而该终止线程也会成为僵尸线程。因此,当我们去连接某个线程时,其实也是在告诉系统该终止线程的资源可以回收了。

注意:对于一个已经被连接过的线程再次执行连接操作, 将会导致无法预知的行为!

线程的分离
有时我们并不在乎某个线程是不是已经终止了,我们只是希望如果某个线程终止了,系统能自动回收掉该终止线程所占用的资源。pthread_detach 函数为我们提供了这个功能,该功能称为线程的分离:

#include <pthread.h>
int pthread_detach(pthread_t thread);

参数说明:

thread(输入参数),指定希望执行分离操作的线程
返回值:

  • 成功时,返回 0
  • 错误时,返回正数错误码

默认情况下,一个线程终止了,是需要在被连接后系统才能回收其占有的资源的。如果我们调用 pthread_detach 函数去分离某个线程,那么该线程终止后系统将自动回收其资源。

注意:一个线程如果已经被分离了,那么我们就无法再去连接它了!

基本操作代码

/*
* 文件名: thread_sample1.c
* 描述:演示线程基本操作
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

/*子线程1入口函数*/
void *thread_routine1(void *arg)
{
    fprintf(stdout, "thread1: hello world!\n");
    sleep(1);
    /*子线程1在此退出*/
    return NULL;
}

/*子线程2入口函数*/
void *thread_routine2(void *arg)
{

    fprintf(stdout, "thread2: I'm running...\n");
    pthread_t main_thread = (pthread_t)arg;

    /*分离自我,不能再被连接*/
    pthread_detach(pthread_self());

    /*判断主线程ID与子线程2ID是否相等*/
    if (!pthread_equal(main_thread, pthread_self())) {
        fprintf(stdout, "thread2: main thread id is not equal thread2\n");
    }

    /*等待主线程终止*/
    pthread_join(main_thread, NULL);
    fprintf(stdout, "thread2: main thread exit!\n");

    fprintf(stdout, "thread2: exit!\n");
    fprintf(stdout, "thread2: process exit!\n");
    /*子线程2在此终止,进程退出*/
    pthread_exit(NULL);
}

int main(int argc, char *argv[])
{

    /*创建子线程1*/
    pthread_t t1;
    if (pthread_create(&t1, NULL, thread_routine1, NULL)!=0) {
        fprintf(stderr, "create thread fail.\n");
        exit(-1);
    }
    /*等待子线程1终止*/
    pthread_join(t1, NULL);
    fprintf(stdout, "main thread: thread1 terminated!\n\n");

    /*创建子线程2,并将主线程ID传递给子线程2*/
    pthread_t t2;
    if (pthread_create(&t2, NULL, thread_routine2, (void *)pthread_self())!=0) {
        fprintf(stderr, "create thread fail.\n");
        exit(-1);
    }

    fprintf(stdout, "main thread: sleeping...\n");
    sleep(3);
    /*主线程使用pthread_exit函数终止,进程继续存在*/
    fprintf(stdout, "main thread: exit!\n");
    pthread_exit(NULL);

    fprintf(stdout, "main thread: never reach here!\n");
    return 0;
}

编译及运行:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值