编程使用函数<二>

目录

前言:

pthread_mutex_init()

pthread_cond_init()

pthread_create()


前言:

        总结一些编程时遇到的函数,会依次按照所需包含头文件、功能、返回值和参数的顺序来介绍,资料参考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函数中作为参数。


感谢阅读

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值