C语言 pthread_create

蹲街弑〆低调 提交于 2020-02-08 12:56:36

基础

  1. 注意编译多线程程序的时候要添加-lpthread参数
  2. int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
    参数1:初始化线程类型的变量
    参数2:通常传NULL,表示使用线程默认属性。
    参数3:函数指针,该函数运行结束,则线程结束。
    参数4:函数的参数

返回值:成功:0; 失败:错误号

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

void *tfn(void *arg)//线程函数
{
	printf("线程 Thread_ID = %lu\n", pthread_self());
	return NULL;
}

int main(void)
{
	pthread_t tid;

	pthread_create(&tid, NULL, tfn, NULL);//创建线程
	sleep(1);//等待线程结束
	printf("进程,pid = %d\n", getpid());

	return 0;
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!