对于pthread 的函数。最常用的情况下,只要用 pthread_create 和 pthread_join 即可。如果想深入了解使用的话,可参考宝典《Unix 环境高级编程》,床头书之一。
说起来,买了不少的床头书了。阅读习惯一直没有养成。只有看小说的习惯。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/param.h>
#include <pthread.h>
int test_handle(void *argc)
{
printf("Enter test handle\n");
while (1) {
}
printf("ccc\n");
}
int main(int argc, char *argv[])
{
pthread_t p;
printf("start!!\n");
pthread_create(&p, NULL, (void *)test_handle, NULL);
printf("over\n");
sleep(2);
printf("join\n");
pthread_join(p, NULL);
printf("bbb");
return 0;
}pthread_join 会挂起当前的进程,直至 p 完成(在本例中的情况是这样,当然,还有线程分离的作法)。
学习是一种积累,日常的点滴汇聚而成。
本文介绍了pthread库中线程创建及同步的基本方法,通过一个简单的C语言示例程序展示了如何使用pthread_create和pthread_join函数来创建并同步线程。
2310

被折叠的 条评论
为什么被折叠?



