浅谈linux - 线程的基本应用

概述

线程(英语:thread)是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。

注意

线程和进程之间的区别

1. 线程是执行的基本单位;进程是资源分配的基本单位。

2. 线程共享进程的资源,一个进程中至少有一个线程. 主线程。

3. 进程有自己的pid,还有自己的PCB;线程有自己的tid,也有自己的TCB。

4. 线程也有自己私有的资源。

另外,小编所有文章均是自己亲手编写验证,由于文件太多,小编就不在公众号后台一一回复列举了,若需要小编的工程代码,请关注公众号:不只会拍照的程序猿,后台回复需要的工程文件。小编看到后会第一时间回复。

接口

线程创建 

进程被创建时,系统会为其创建一个主线程,而要在进程中创建新的线程,则可以调用pthread_create。另外创建新的线程以后,新的线程和原来的线程是异步的。

/**
 * @ 创建一个新的线程 "pthread_create(3)"
 * @ thread: 新创建的线程的id存储到这里        attr:NULL表明线程创建的时候,使用默认属性
 * @ start_routine: 线程的执行函数            arg:传递给线程执行函数的唯一的参数
 * @ 成功返回0;错误返回一个错误号errno被设置
 */
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                  void *(*start_routine) (void *), void *arg);

线程终止

线程终止有三种方式。

方式一:执行完成后隐式退出。

方式二:由线程本身显示调用pthread_exit 函数退出。

/**
 * @ 终止当前线程 "pthread_exit(3)"
 * @ retval: 通过这个参数返回一个值,这个值被同一进程中的另外一个线程调用pthread_join(3)使用
 */
void pthread_exit(void *retval);

方式三:被其他线程用pthread_cance函数终止。

/**
 * @ 给一个线程发送取消请求 "pthread_cancel(3)"
 * @ thread: 指定了目标线程的id
 * @ 成功返回0;错误返回非0错误码
 */
int pthread_cancel(pthread_t thread);

线程汇合

主线程可以等待子线程终止并与之汇合后继续运行,子线程终止后主线程将回收该线程的相关资源。

/**
 * @ 汇合一个终止的线程 "pthread_join(3)"
 * @ thread: 指定了要汇合的线程的id.目标线程的id
 * @ retval: 退出状态码被保存到*retval中.如果是被取消的线程,PTHREAD_CANCELED---->*retval中
 * @ 成功返回0;错误返回一个错误号
 */
int pthread_join(pthread_t thread, void **retval);

线程分离

/**
 * @ 分离一个线程 "pthread_detach(3)"
 * @ thread: 指定了要分离的线程的id
 * @ 成功返回0;错误返回一个错误号
 */
int pthread_detach(pthread_t thread);

获取线程的tid

/**
 * @ 获取当前线程的id "pthread_self(3)"
 * @ 返回当前的线程的id
 */
pthread_t pthread_self(void);

示例

★示例通过pthread_test.c向用户展示线程的基本使用。

★包含演示程序pthread_test.c(已验证通过)。

e1df3a50d127e6cd10251eb73be0fbf7.png pthread_test.c

/**
 * @Filename : pthread_test.c
 * @Revision : $Revision: 1.00 $
 * @Author : Feng(更多编程相关的知识和源码见微信公众号:不只会拍照的程序猿,欢迎订阅)
 * @Description : 线程的基本应用示例
**/

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

/**
 * @ 线程执行函数
 * @ args: 传递来的参数
 */
static void *thread1(void *args)
{
    /* 1. 获取参数信息 */
    printf("%s\n", (char *)args);

    /* 2. 获取当前线程id */
    pthread_t pthread_self (void);
    printf("tid is %lu\n", (int)pthread_self());

    sleep(3);
    return (void *)7;
}


/**
 * @ 主函数,程序入口
 */
int main(void)
{
    void *ret;
    pthread_t tid;      /* 线程ID */

    /* 创建线程 */
    if (pthread_create(&tid, NULL, thread1, "feng") != 0) {
        printf("pthread create failed...\n");
        return -1;
    }

    /* 阻塞等待线程的汇合,接收线程的退出状态码 */
    pthread_join(tid, &ret);
    printf("thread1 exit code %d\n", (int)ret);

    return 0;
}

验证

编译程序,记得加库-pthread

#编译代码,记得-pthread
ubuntu@U:~/study/pthread$ gcc pthread_test.c -pthread
ubuntu@U:~/study/pthread$

执行程序

#执行代码
ubuntu@U:~/study/pthread$ ./a.out 
feng
tid is 2908280576
thread1 exit code 7
ubuntu@U:~/study/pthread$

往期 · 推荐

实时系统vxWorks - 任务(重要)

实时系统vxWorks - 加载应用程序的方法

实时系统vxWorks - 在线调试

实时系统vxWorks - 虚拟机环境搭建

实时系统vxWorks - zynq7020移植vxWorks

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不只会拍照的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值