1、三个基本API
extern int pthread_create __P ((pthread_t *__thread, __const pthread_attr_t *__attr, void *(*__start_routine) (void *), void *__arg));
第一个参数为指向线程标识符的指针,后续可以使用该线程标示符来处理线程
第二个参数用来设置线程属性,为NULL,则生成默认属性线程
第三个参数是线程运行函数的起始地址
第四个参数是运行函数的参数,若没有则传入NULL
返回值:
0:成功
EAGAIN:线程数目过多
EINVAL:线程属性值非法
extern int pthread_join __P ((pthread_t __th, void **__thread_return));
第一个参数为被等待的线程标识符
第二个参数为一个用户定义的指针,它可以用来存储被等待线程的返回值
extern void pthread_exit __P ((void *__retval)) __attribute__ ((__noreturn__));
唯一的参数是函数的返回代码
2、举例
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
void thread(void)
{
int i;
for (i=0;i<3;i++)
printf("This is a pthread.\n");
}
int main(void)
{
pthread_t id;
int i,ret;
ret=pthread_create(&id,NULL,(void *) thread,NULL);
if (ret != 0){
printf ("Create pthread error!\n");
exit (1);
}
for(i=0;i<3;i++)
printf("This is the main process.\n");
pthread_join(id,NULL);
}
来源:CSDN
作者:学无止境966
链接:https://blog.csdn.net/qq_23929673/article/details/98479123