我是靠谱客的博主 受伤毛豆,最近开发中收集的这篇文章主要介绍线程创建等待及退出,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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

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

void *thread_func(void arg)
{
static int ret = 10;
printf(“t1: %ld thread is createn”,(unsigned long)pthread_self());
printf(“t1:param is %dn”,
((int *)arg));
pthread_exit((void *)&ret);

}
int main()
{
int ret;
int param = 10;
pthread_t t1;
int *pret = NULL;
ret = pthread_create(&t1,NULL,thread_func,(void *)&param);
if(ret == 0){
printf(“main: create t1 successn”);
}
printf(“main:%ldn”,(unsigned long)pthread_self());
pthread_join(t1,(void **)&pret);
printf(“main: t1 quit: %dn”,*pret);
return 0;
}
2.返回字符串
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>

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

void *thread_func(void *arg)
{
static int ret = 10;
static char p = “t1 is run out”;
printf(“t1: %ld thread is createn”,(unsigned long)pthread_self());
printf(“t1:param is %dn”,
((int *)arg));
pthread_exit((void *)p);

}
int main()
{
int ret;
int param = 10;
pthread_t t1;
char *pret = NULL;
ret = pthread_create(&t1,NULL,thread_func,(void *)&param);
if(ret == 0){
printf(“main: create t1 successn”);
}
printf(“main:%ldn”,(unsigned long)pthread_self());
pthread_join(t1,(void **)&pret);
printf(“main: t1 quit: %sn”,pret);
return 0;
}

最后

以上就是受伤毛豆为你收集整理的线程创建等待及退出的全部内容,希望文章能够帮你解决线程创建等待及退出所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部