目录
前言:
总结一些编程时遇到的函数,会依次按照所需包含头文件、功能、返回值和参数的顺序来介绍,资料参考man手册和互联网。
pthread_mutex_init()
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
头文件:#include <pthread.h>
功能:用于初始化互斥锁(mutex)。
返回值:成功返回0,失败设置错误码;
参数:
第一个参数(pthread_mutex_t* restrict mutex):在调用该函数前声明并分配的需要初始化的互斥锁。
第二个参数(const pthread_mutexattr_t *restrict attr):用于设置互斥锁属性的,可以用来改变互斥锁的行为。如果该参数为NULL则表示使用默认属性的互斥锁(多数情况).
pthread_cond_init()
int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
头文件:#include <pthread.h>
功能:初始化条件变量。
参数:
第一个参数(pthread_cond_t *restrict cond):在调用该函数前声明并分配的需要初始化的条件变量。
第二个参数(pthread_mutexattr_t *restrict attr):用于设置条件变量的属性。大多数情况下该参数为NULL,表示使用默认条件变量属性。
pthread_create()
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
头文件:#include <pthread>
功能:创建一个线程。这个线程用于执行指定任务。
参数:
第一个参数(pthread_t *thread):一个指向pthread_t类型变量的指针,该变量用于存储新创建的线程的标识符。
第二个参数(pthread_attr_t *attr):用于设置新线程的属性,例如线程栈的大小,调度策略等。这个参数为NULL时,则使用默认的线程属性。
第三个参数(void*(*start_routine)(void*)):一个指向新线程将要执行的函数的指针,该函数参数为 void*,返回值为 void*。
第四个参数( void* arg):传入第三个参数start_routine函数中作为参数。
感谢阅读