pthread 基础篇 创建线程

本文介绍了POSIX线程库中pthread_create函数的基本用法,包括函数原型、参数说明及一个简单的示例程序。通过该示例展示了如何创建一个新的线程并指定其执行的函数。

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

1· 创建线程

    int pthread_create(pthread_t * thread, 
                       const pthread_attr_t * attr,
                       void * (*start_routine)(void *), 
                       void *arg);
thread: 创建线程后返回的相乘ID

attr:创建线程的属性,若为NULL,则按默认属性创建

start_routine:线程回调函数,即线程执行时调用的函数

arg:指向回调函数时传入的参数


基本用法:

void *thread1_proc_function(void *arg)

{

printf("i am sub thread!\n");

return NULL;

}


void main(void)

{

int iret = 0;

pthread_t thread1;

iret = pthread_create(&thread1, NULL, thread1_proc_function, NULL);

if(0 != iret)

{

printf("create pthread error");

}

exit(0);

}


参考资料:

1· http://zh.wikipedia.org/wiki/Native_POSIX_Thread_Library

2· http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html#BASICS

3· http://learn.akae.cn/media/ch35s03.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值