pthread 函数中create、join 、detach的代码实现和运行结果

本文详细介绍了pthread库中的create、join和detach函数,通过示例代码展示了如何在C语言中实现多线程操作。在create.c中,展示了线程交替运行的场景;join.c中,解释了pthread_join如何回收线程并复用地址,当线程被取消时,返回值为PTHREAD_CANCELED;detach.c中,强调了pthread_detach的调用时机,确保其在线程join之前执行。

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

create.c

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

void* Entry1(void* arg)
{
   (void)arg;
   while(1)
   {
     printf("hello world1!\n");
     sleep(1);
   }
   return NULL;   
}

void* Entry2(void* arg)
{
   (void)arg;
   while(1)
   {
     printf("hello world2!\n");
     sleep(1);
   }
   return NULL;   
}

int main()
{
    pthread_t tid1, tid2;
    pthread_create(&tid1, NULL, Entry1, NULL);
    pthread_create(&tid2, NULL, Entry2, NULL);
    while(1)
    {
     printf("Main is here.\n");
     sleep(1);
    }

    return 0;
}

运行结果:三个线程通过cpu的调度交替运行。

join.c

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


void *thread1(void* arg)
{
    (void)arg;
    printf("I am thread1.\n");
    int *p=(int*)malloc(sizeof(int));
    *p=1;
    return (void*)p;
}

void * thread2(void* arg)
{
    (void)arg;
    printf("I am thread2.\n");
    int *p = (int*)malloc(sizeof(int));
    *p=2;
    return (void*)p;
}

void * thread3(void* arg)
{
    (void)arg;
     while(1)
     {
         printf("I am thread3.\n");
         sleep(1);
     }
    return NULL;
}
int main()
{
  pthread_t tid;
  void * ret;

  pthread_create(&tid,NULL, thread1, NULL);
  pthread_join(tid,&ret);
  printf("The thread is return, id %X, return value:%d \n",(unsigned int)tid, *(int*)ret);
  free(ret);

  pthread_create(&tid,NULL, thread2, NULL);
  pthread_join(tid,&ret);
  printf("The thread is return, id %X, return value:%d \n",(unsigned int)tid, *(int*)ret);
  free(ret);

  pthread_create(&tid,NULL, thread3, NULL);
  sleep(2);
  pthread_cancel(tid);
  pthread_join(tid,&ret);

  if(ret==PTHREAD_CANCELED)
  {

  printf("The thread is return, id %X, return value:PTHREAD_CANCELED\n",(unsigned int)tid);
  }
  else
  {
      printf("The thread is return, id %X, return code :NULL\n",(unsigned int)tid);
  }

  return 0;
}

运行结果:通过pthread_join函数回收已经结束的线程,之后再次创建线程时该地址被复用。当线程被pthread_cancel终止后,

value 的值等于系统定义的宏 PTHREAD_CANCELED (注意是 value 不是 *value)

detach.c

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

void* thread(void* arg)
{
    pthread_detach(pthread_self());
    printf("%s \n",(char*)arg);
    return NULL;
}
int main()
{
  pthread_t tid;
  pthread_create(&tid, NULL, thread, (void*)"hello world.");
  
   int ret; 
   sleep(1);  
    ret=pthread_join(tid, NULL);
   
   if(ret==0)
   {
     printf("waiting success.\n");
   }
   else
   { 
       printf("waiting failed\n");
   }
   

  return ret;
}

运行结果: sleep(1)保证 pthread_detach 先于 pthread_join 被调用。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值