🌟linux pthread_create()函数用法🌟
在Linux系统中,`pthread_create()` 是线程编程中的重要函数之一,用于创建一个新的线程。它的原型如下:`int pthread_create(pthread_t thread, const pthread_attr_t attr, void (start_routine) (void ), void arg);`
首先,需要包含头文件 `
例如:
```c
include
void thread_func(void arg) {
printf("Hello from thread! %d\n", (int)arg);
return NULL;
}
int main() {
pthread_t thread_id;
int num = 42;
pthread_create(&thread_id, NULL, thread_func, &num);
pthread_join(thread_id, NULL);
return 0;
}
```
运行后会看到子线程打印出 "Hello from thread!" 和传入的数值。掌握 `pthread_create()` 的使用,是实现多线程编程的基础,让程序并发执行提高效率!🚀
免责声明:本答案或内容为用户上传,不代表本网观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。 如遇侵权请及时联系本站删除。