概述
#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 *)¶m);
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 *)¶m);
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;
}
最后
以上就是受伤毛豆为你收集整理的线程创建等待及退出的全部内容,希望文章能够帮你解决线程创建等待及退出所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复