pthread_setconcurrency函数用法详解
pthread_setconcurrency函数简介
- 头文件包含
#include <pthread.h>
- 函数定义
int pthread_setconcurrency(int new_level );
int pthread_getconcurrency( void );
- 编译链接选项
-pthread
pthread_setconcurrency函数常见使用错误
- 链接错误
undefined reference to `pthread_setconcurrency'
解决办法:添加链接选项
-pthread
- 编译错误
warning: implicit declaration of function ‘pthread_setconcurrency’ [-Wimplicit-function-declaration]
解决办法:包含头文件
#include <pthread.h>
pthread_setconcurrency函数详细描述
pthread_setconcurrency ()函数通知实现应用程序所需的并发级别(在new_level 中指定)。实现仅将此作为一个提示:POSIX.1没有指定调用pthread_setconcurrency ()后应提供的并发级别
将new_level指定为0指示实现管理它认为合适的并发级别。
pthread_getconcurrency ()返回此进程并发级别的当前值。
pthread_setconcurrency函数返回值
成功时,pthread_setconcurrency ()返回0;出错时,它返回一个非零错误号。
如果以前没有调用过pthread_setconcurrency (),则pthread_getconcurrency ()总是成功,返回以前调用pthread_setconcurrency ()或0所设置的并发级别。
pthread_setconcurrency函数错误码
pthread_setconcurrency ()可能会失败,出现以下错误:
- EINVAL new_level为负值。
POSIX.1还记录了一个EAGAIN错误(“new_level指定的值将导致超出系统资源”)。
pthread_setconcurrency函数其他说明
默认并发级别为0。
并发级别只对m:n个线程实现有意义,在任何时刻,进程的用户级线程集的子集都可能绑定到较少数量的内核调度实体。设置并发级别允许应用程序向系统提供一个提示,说明应该为应用程序的有效执行提供的内核调度实体的数量。
LinuxThreads和NPTL都是1:1线程实现,因此设置并发级别没有意义。换句话说,在Linux上,这些函数只是为了与其他系统兼容而存在的,它们对程序的执行没有影响。
评论区