pthread_setcurrency生效问题

clark2009 2012-12-03 11:43:01
UNPV2 129页set_concurrency无法生效。我的虚拟机是单核,Ubuntu 2.6.32-36-generic。pthread_getconcurrency为0,set以后返回值正确;再次get也是正确的。程序执行还是单线程执行。
看网上说,多核涉及到绑定CPU,不是很理解。 我的是单核,应该不存在绑定CPU的问题。哪位大侠科普一下


#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sched.h>

#define MAXITEMS 100*10000
#define MAXTHREADS 100
#define min(a, b) a<b?a:b

int nitems;
struct {
pthread_mutex_t mutex;
int buff[MAXITEMS];
int nput;
int nval;
}shared = {PTHREAD_MUTEX_INITIALIZER};
void *producer(void*), *consumer(void*);


struct Node{
int count;
int num;
};

int main(int argc, char **argv)
{
int i, nthreads;
struct Node count[MAXTHREADS];

pthread_t tid_producer[MAXTHREADS], tid_consumer;

if(argc != 3)
printf("usage: %s items threads!\n", argv[0]),exit(0);


nitems = min(atoi(argv[1]), MAXITEMS);
nthreads = min(atoi(argv[2]), MAXTHREADS);
printf("nitems=%d, nthreads=%d\n", nitems, nthreads);


//for more CPUS, must bind to one cpu
int level = pthread_getconcurrency();
printf("getconcurrency = %d\n", level);

if(pthread_setconcurrency(nthreads) != 0)
printf("setconcurrency error:%d,%s\n", errno, strerror(errno)), exit(0);
else{
level = pthread_getconcurrency(nthreads);
printf("after set=%d, getconcurrency = %d\n", nthreads, level);
}



for(i=0;i<nthreads;++i){
count[i].num = i;
count[i].count = 0;
if(pthread_create(&tid_producer[i], NULL, producer, &count[i]) != 0)
printf("create pthread %d error: %d,%s\n", i, errno, strerror(errno)), exit(0);
}

for(i=0;i<nthreads;++i){
pthread_join(tid_producer[i], NULL);
printf("[%d] num[%d] = %d\n", i, count[i].num, count[i].count);
}


pthread_create(&tid_consumer, NULL, consumer, NULL);
pthread_join(tid_consumer, NULL);

// sleep(100);

return 0;
}


void* producer(void *arg)
{
pthread_t pid = pthread_self();
// printf("create pthread %x, val=%d\n", (unsigned int)arg, *(int*)arg);
// sleep(1);
struct Node *p = (struct Node*)arg;
printf("num[%d] = %d\n", p->num, p->count);
while(1){
pthread_mutex_lock(&shared.mutex);
if(shared.nput >= nitems){
pthread_mutex_unlock(&shared.mutex);
return NULL;
}

shared.buff[shared.nput] = shared.nval;
++shared.nput;
++shared.nval;
// printf("running pthread %x\n", (unsigned int)arg);
pthread_mutex_unlock(&shared.mutex);
p->count++;
}

}


void* consumer(void* arg)
{
int i;
for(i=0;i<nitems;++i){
if(shared.buff[i] != i)
printf("buff[%d] = %d\n", i, shared.buff[i]);
}

return NULL;
}




我测试,超过四个线程以后,总是第四个线程执行
...全文
123 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
clark2009 2012-12-04
  • 打赏
  • 举报
回复
哈哈,英文不好。大概意思是只是为了兼容其他系统,没有实际意义是吧。 谢谢,
nevil 2012-12-04
  • 打赏
  • 举报
回复
Linux? 建议好好看一下man The default concurrency level is 0. Concurrency levels are only meaningful for M:N threading implementations, where at any moment a subset of a process's set of user-level threads may be bound to a smaller number of kernel-scheduling entities. Setting the concurrency level allows the application to give the system a hint as to the number of kernel-scheduling entities that should be provided for efficient execution of the application. Both LinuxThreads and NPTL are 1:1 threading implementations, so setting the concurrency level has no meaning. In other words, on Linux these functions merely exist for compatibility with other systems, and they have no effect on the execution of a program.

23,125

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧