everyday one code-multthread

本文提供了一个在Ubuntu Linux系统中使用C语言创建和管理线程的示例程序。该程序展示了如何初始化子线程、实现主线程与子线程间的同步以及返回值的处理。通过具体代码实现了子线程的运行、停止及退出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

is being running at ubunte linux system.

dusibo hunan yongzhou

 

 

 

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

static int run=1;
static int retvalue;

void *start_routine(void *arg)
{
    int *running=(int *)arg;
    printf("child thread initation complete %d/n",*running);


    while(*running)
    {
        printf("child thread running/n");

        usleep(1);
    }

    printf("child thread out/n");

    retvalue=8;

    pthread_exit((void**)retvalue);
}


int main(void)
{
    pthread_t pt;
    int ret=-1;
    int time=3;
    int i=0;
    int *ret_join=NULL;

    ret=pthread_create(&pt,NULL,start_routine,&run);

    if(ret!=0)
    {
        printf("build a thread/n");

        return 1;
    }

    usleep(1);   

    for(;i<time;i++)
    {
        printf("root thread print/n");

        usleep(1);
    }


    run=0;
    pthread_join(pt,(void**)ret_join);
    printf("bak value is %d/n",*ret_join);

    return 0;
}

 

 

杜思波 2011 湖南

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值