- 引入头文件:
#include <pthread.h> //开启线程相关头文件#include <stdio.h> - 编写线程函数:
void* tprocess(void * args){
//运行程序体
return NULL;} //注意是函数指针 - 调用线程函数:
pthread_t t; //用于声明线程ID
pthread_create(&t,NULL,tprocess,NULL); //创建线程,线程创建了就开始运行。
pthread_join(t,NULL); //等待线程结束,用来等待一个线程的结束,线程间同步的操作。
**int pthread_join(pthread_t thread, void retval);
参数 :thread: 线程标识符,即线程ID,标识唯一线程。retval: 用户定义的指针,用来存储被等待线程的返回值。
返回值 : 0代表成功。 失败,返回的则是错误号。
pthread_mutex_lock
当pthread_mutex_lock()返回时,该互斥锁已被锁定。线程调用该函数让互斥锁上锁,如果该互斥锁已被另一个线程锁定和拥有,则调用该线程将阻塞,直到该互斥锁变为可用为止。

4265

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



