使用Pthread互斥锁同步多个线程时遇到问题

时间:2016-03-01 02:53:56

标签: c synchronization locking pthreads mutex

我正在尝试设置一个程序,其中将生成一些线程(在命令行中指定)。每个线程将重复请求一定数量的有限数量(在这种情况下为5)可用资源。然后,它将按照收到的资源数量(即:3个资源被授予 - > 3秒休眠)休眠几秒钟,然后将资源返回到池中。

我已经设置了increaseCount()decreaseCount()过程,目的是要求一个线程获取一个名为lock的pthread互斥锁,然后才能访问全局变量available resources ,它跟踪5个池中有多少资源可供使用。

目前,我的程序会发出一个或几个已授予的请求,但会快速耗尽资源并匆匆忙忙地打印出数千条insufficient resources...条消息。

任何人都可以看到我程序背后的逻辑存在明显缺陷吗?

//------------------------------------------------------------------------
void * threadProcedure(void * i){
int a = *((int *) i);
do{
    /* Generate a random number [1, MAX_RESOURCES] of resources to request */
    time_t t;
    srand((int)time(&t) % a);
    int requestNum = (rand() % MAX_RESOURCES) + 1;

    /* Request that many resources */
    resourcesReceived = decreaseCount(requestNum);
    /* Sleep for a time (in seconds) proportional to the number of resources requested */
    sleep(resourcesReceived);
    /* Return the resources */
    if(resourcesReceived > 0){ increaseCount(resourcesReceived); }

}while(1);
}//end threadProcedure
//------------------------------------------------------------------------
int decreaseCount(int x){
pthread_mutex_lock(&lock); //Acquire the lock

//If the requested number of resources aren't available
if(x > availableResources){
    printf("Insufficient resources. Requested: %d Available: %d\n", x, availableResources);
    pthread_mutex_unlock(&lock); //Reset the lock
    return 0; //Return without allocating any resources
}

//If the requested resources ARE available
else{
    printf("Allocating. %d resources. ", x);
    availableResources -= x; //Allocate resources
    printf("availableResources: %d\n", availableResources);
    pthread_mutex_unlock(&lock); //Free (unlock) the lock
    return x;
}
}//end decreaseCount
//------------------------------------------------------------------------
int increaseCount(int x){
pthread_mutex_lock(&lock); //Acquire the lock

availableResources += x; //Update the number of available resources
printf("availableResources: %d\n\n", availableResources);

pthread_mutex_unlock(&lock); //Free (unlock) the lock

return 0;
}//end increaseCount

更新:我尝试使用pthread_cond_wait()pthread_cond_signal来缓解问题:(输出仍然打印得非常快/不停)

//------------------------------------------------------------------------
int decreaseCount(int x){
pthread_mutex_lock(&lock); //Acquire the lock

pthread_cond_wait(&cond, &lock);

//If the requested number of resources aren't available
if(x > availableResources){
    printf("Requested: %d, Available: %d. Denied.\n", x, availableResources);
    pthread_mutex_unlock(&lock); //Reset the lock
    return 0; //Return without allocating any resources
}

//If the requested resources ARE available
else{
    printf("Requested: %d, Available: %d. Allocated.\t",x, availableResources);
    availableResources -= x; //Allocate resources
    printf("availableResources: %d\n", availableResources);
    pthread_mutex_unlock(&lock); //Free (unlock) the lock
    return x;
}
}//end decreaseCount
//------------------------------------------------------------------------
int increaseCount(int x){
pthread_mutex_lock(&lock); //Acquire the lock

pthread_cond_signal(&cond);

availableResources += x; //Update the number of available resources
printf("%d resources returned. availableResources: %d\n\n", x, availableResources);

pthread_mutex_unlock(&lock); //Free (unlock) the lock

return 0;
}//end increaseCount

我的输出如下:

Requested: 2, Available: 5. Allocated.  availableResources: 3
Requested: 2, Available: 3. Allocated.  availableResources: 1
Requested: 1, Available: 1. Allocated.  availableResources: 0
Requested: 2, Available: 0. Denied.
Requested: 2, Available: 0. Denied.
2 resources returned. availableResources: 2

2 resources returned. availableResources: 4

1 resources returned. availableResources: 5

Requested: 2, Available: 5. Allocated.  availableResources: 3
Requested: 2, Available: 3. Allocated.  availableResources: 1
Requested: 2, Available: 1. Denied.

0 个答案:

没有答案