c语言系统编程之线程

线程是CPU调度的基本单位
进程是系统分配资源的基本单位

线程依赖于进程,线程共享进程的资源
一个进程可以有多个线程

线程的创建

 #include <pthread.h>

       int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
        void *(*start_routine) (void *), void *arg);

功能:
创建一个线程。

参数:
thread:线程标识符地址。
attr:线程属性结构体地址。
start_ routine: 线程函数的入口地址。
arg:传给线程函数的参数。

返回值:
成功:返回0
失败:返回非0

等待线程结束

int pthread_join(pthread_t thread, void **retval);

功能:
等待子线程结束,并回收子线程资源。.

参数:
thread:被等待的线程号。
retval:用来存储线程退出状态的指针的地址。

返回值:
成功返回0,失败返回非0。
例子:

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

//线程处理函数
void *deal_fun(void *arg)
{   
    int i = 0;
    for(i = 0; i < 10; i++)
    {
        printf("线程号%lu,i = %d\n",pthread_self(),i);
        sleep(1);
    }

}

int main(int argc, char const *argv[])
{
    //定义线程号
    pthread_t tid1,tid2;
    
    //创建两个线程
    pthread_create(&tid1,NULL,deal_fun,NULL);
    pthread_create(&tid2,NULL,deal_fun,NULL);
  
    //阻塞线程
    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);

    return 0;
}

运行结果:
线程号140383015573248,i = 0
线程号140383023965952,i = 0
线程号140383015573248,i = 1
线程号140383023965952,i = 1
线程号140383015573248,i = 2
线程号140383023965952,i = 2
线程号140383015573248,i = 3
线程号140383023965952,i = 3
线程号140383015573248,i = 4
线程号140383023965952,i = 4
线程号140383015573248,i = 5
线程号140383023965952,i = 5
线程号140383015573248,i = 6
线程号140383023965952,i = 6
线程号140383015573248,i = 7
线程号140383023965952,i = 7
线程号140383015573248,i = 8
线程号140383023965952,i = 8
线程号140383015573248,i = 9
线程号140383023965952,i = 9

线程分离

     int pthread_detach(pthread_t thread);

创建一个线程后应回收其资源,但使用pthread_join函数会使调用者阻塞,故Linux提供了线程分离函数

功能:使调用线程与当前进程分离,使其成为-一个独立的线程,该线程终止时,系统将自动回收它的资源。

参数:thread:线程号

返回值:成功:返回0,失败返回韭0。

例子:

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

//线程处理函数
void *deal_fun(void *arg)
{   
    int i = 0;
    for(i = 0; i < 5; i++)
    {
        printf("线程号%lu,i = %d\n",pthread_self(),i);
        sleep(1);
    }

}

int main(int argc, char const *argv[])
{
    //定义线程号
    pthread_t tid1,tid2;
    
    //创建两个线程
    pthread_create(&tid1,NULL,deal_fun,NULL);
    pthread_create(&tid2,NULL,deal_fun,NULL);
    
    //线程分离
    pthread_detach(tid1);
    printf("线程1分离\n");
    pthread_detach(tid2);
    printf("线程2分离\n");

    //阻塞
    while(1);
 
    return 0;
}

运行结果:
线程1分离
线程2分离
线程号140261347002112,i = 0
线程号140261355394816,i = 0
线程号140261347002112,i = 1
线程号140261355394816,i = 1
线程号140261347002112,i = 2
线程号140261355394816,i = 2
线程号140261347002112,i = 3
线程号140261355394816,i = 3
线程号140261347002112,i = 4
线程号140261355394816,i = 4
^C

多线程例子:

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

//线程处理函数
void *deal_fun(void *arg)
{   
    int i = 0;
    for(i = 0; i < 5; i++)
    {
        printf("线程号%lu,i = %d\n",pthread_self(),i);
        sleep(1);
    }

}

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

    int n = 0;
    printf("输入要创建的线程个数n = %d\n",n);
    scanf("%d",&n);

    int i = 0;
    for ( i = 0; i < n; i++)
    {
    //定义线程号
    pthread_t tid1;
    //创建线程
    pthread_create(&tid1,NULL,deal_fun,NULL); 

    //线程分离
    pthread_detach(tid1);
    }
    
    //阻塞
    while(1);
    return 0;
}

运行结果:
输入要创建的线程个数n = 0
5
线程号139725122971392,i = 0
线程号139725131364096,i = 0
线程号139725139756800,i = 0
线程号139725148149504,i = 0
线程号139725156542208,i = 0
线程号139725122971392,i = 1
线程号139725131364096,i = 1
线程号139725139756800,i = 1
线程号139725148149504,i = 1
线程号139725156542208,i = 1
线程号139725122971392,i = 2
线程号139725131364096,i = 2
线程号139725139756800,i = 2
线程号139725148149504,i = 2
线程号139725156542208,i = 2
线程号139725122971392,i = 3
线程号139725131364096,i = 3
线程号139725139756800,i = 3
线程号139725148149504,i = 3
线程号139725156542208,i = 3
线程号139725122971392,i = 4
线程号139725131364096,i = 4
线程号139725139756800,i = 4
线程号139725148149504,i = 4
线程号139725156542208,i = 4
^C
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值