线程-01

本文探讨了C/C++中使用pthread创建线程01、02和03进行并发,并展示了如何在test04函数中管理和同步这些线程。同时,介绍了嵌入式函数thr_run的初始化和数学运算部分,如sqrt函数的应用。
摘要由CSDN通过智能技术生成
#include <pthread.h>
#include <stdio.h>
#include <string.h>
pthread_t tid1,tid2,tid3;

void* pthread_01(void* arg ){
    printf("线程函数1\n");
    return 0;//不返回值 就报错
}

void* pthread_02(){
    printf("线程函数2\n");
    return 0;
}

void* pthread_03(){
    printf("线程函数3\n");
    return 0;
}

int test04(void) {
    pthread_create(&tid1,NULL,pthread_01,NULL);
    pthread_create(&tid2,NULL,pthread_02,NULL);
    pthread_create(&tid3,NULL,pthread_03,NULL); 
    pthread_join(tid1,NULL);//不使用pthread_join看不到线程函数的输出。
    pthread_join(tid2,NULL);
    pthread_join(tid3,NULL);
    return 0;
}

----------------------------------

#include "test.h"
#include <math.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

void *thr_run(){

    int i = 0;
    while (i< 5) {
        i++;
        printf("cnt=%d  this is thr_run\n",i);
        sleep(3);
    }
    return NULL;
}

int init(){
    int ret;
    pthread_t tid;
    ret = pthread_create(&tid,NULL,thr_run,NULL);
    if(ret){
        printf("pthread_create err\n");
        return -1;
    }
    double result = sqrt(4.0);
    printf("this is  math  result = %lf\n",result);
    pthread_join(tid,NULL);
    printf("Bye!\n");
    return  0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值