linux c 中的条件变量解锁
Condition variable unlocking in linux c
这是我创建的一些代码,只是为了测试一些关于条件变量和互斥锁的一般情况。它应该数到 10,但由于某种原因,工作线程在 printlock 互斥体上不断停止,因为主线程在等待 printcond 时没有醒来。
这是我的输出:
"I have this many threads: 7
This is my 1 time running, and my number is 0!
This is my 2 time running, and my number is 1!"
我一定是遗漏了一些关于条件变量的非常明显的东西。我曾尝试在发出 printcond 信号后解锁工作线程中的 printlock,这使得代码成为 运行,但主线程仅打印最后一个值以获取它。我希望主线程为所有 1-10 打印 "The number I got"。我怎么做?我无法理解这些东西的逻辑结构,我到处寻找解决方案。感谢任何帮助。
pthread_mutex_t varlock;
pthread_mutex_t printlock;
pthread_cond_t printcond;
void *worker(void *args)
{
int * count = args;
int run = 1;
while(1==1)
{
pthread_mutex_lock(&varlock);
if(*count == 10)
{
pthread_mutex_unlock(&varlock);
printf("I am a thread exiting\n");
return NULL;
}
else
{
printf("This is my %d time running, and my number is %d!\n", run, *count);
pthread_mutex_lock(&printlock);
pthread_cond_signal(&printcond);
*count = *count +1;
run++;
}
pthread_mutex_unlock(&varlock);
}
}
int main(int argc, char **argv)
{
int num = 7;
printf("I have this many threads: %d\n", num);
int * count = malloc(sizeof(int));
*count = 0;
if (pthread_mutex_init(&varlock, NULL) != 0)
{
printf("mmult: Failed initializing mutex\n");
return 1;
}
if (pthread_mutex_init(&printlock, NULL) != 0)
{
printf("mmult: Failed initializing mutex\n");
return 1;
}
pthread_mutex_lock(&printlock);
int err=pthread_cond_init(&printcond, NULL);
//Create the threads
pthread_t tid[num];
int i;
for(i = 0; i < num; i++)
{
err = pthread_create(&(tid[i]),NULL,worker, count);
if(err)
{
perror("mmult: Failed creating thread\n");
return 1;
}
}
while(1==1)
{
if(*count == 10)
{
break;
}
pthread_cond_wait(&printcond, &printlock);
printf("The number I got is %d.\n", *count);
pthread_mutex_unlock(&printlock);
}
//Join all the threads
int status;
for(i = 0; i < num; i++)
{
pthread_join(tid[i],(void **)&status);
}
printf("Now that I have finished, the count is %d.\n", *count);
return 0;
}
你有很多错误,但最明显的错误是你的第二个 while(1==1)
循环解锁了互斥量但没有锁定它。所以如果你循环,你将解锁一个已经解锁的互斥量。这是一个错误。
您的第一个 while(1==1)
循环也有问题。您解锁互斥锁只是为了立即再次锁定它。您认为这样做的目的是什么?
这是我创建的一些代码,只是为了测试一些关于条件变量和互斥锁的一般情况。它应该数到 10,但由于某种原因,工作线程在 printlock 互斥体上不断停止,因为主线程在等待 printcond 时没有醒来。 这是我的输出:
"I have this many threads: 7
This is my 1 time running, and my number is 0!
This is my 2 time running, and my number is 1!"
我一定是遗漏了一些关于条件变量的非常明显的东西。我曾尝试在发出 printcond 信号后解锁工作线程中的 printlock,这使得代码成为 运行,但主线程仅打印最后一个值以获取它。我希望主线程为所有 1-10 打印 "The number I got"。我怎么做?我无法理解这些东西的逻辑结构,我到处寻找解决方案。感谢任何帮助。
pthread_mutex_t varlock;
pthread_mutex_t printlock;
pthread_cond_t printcond;
void *worker(void *args)
{
int * count = args;
int run = 1;
while(1==1)
{
pthread_mutex_lock(&varlock);
if(*count == 10)
{
pthread_mutex_unlock(&varlock);
printf("I am a thread exiting\n");
return NULL;
}
else
{
printf("This is my %d time running, and my number is %d!\n", run, *count);
pthread_mutex_lock(&printlock);
pthread_cond_signal(&printcond);
*count = *count +1;
run++;
}
pthread_mutex_unlock(&varlock);
}
}
int main(int argc, char **argv)
{
int num = 7;
printf("I have this many threads: %d\n", num);
int * count = malloc(sizeof(int));
*count = 0;
if (pthread_mutex_init(&varlock, NULL) != 0)
{
printf("mmult: Failed initializing mutex\n");
return 1;
}
if (pthread_mutex_init(&printlock, NULL) != 0)
{
printf("mmult: Failed initializing mutex\n");
return 1;
}
pthread_mutex_lock(&printlock);
int err=pthread_cond_init(&printcond, NULL);
//Create the threads
pthread_t tid[num];
int i;
for(i = 0; i < num; i++)
{
err = pthread_create(&(tid[i]),NULL,worker, count);
if(err)
{
perror("mmult: Failed creating thread\n");
return 1;
}
}
while(1==1)
{
if(*count == 10)
{
break;
}
pthread_cond_wait(&printcond, &printlock);
printf("The number I got is %d.\n", *count);
pthread_mutex_unlock(&printlock);
}
//Join all the threads
int status;
for(i = 0; i < num; i++)
{
pthread_join(tid[i],(void **)&status);
}
printf("Now that I have finished, the count is %d.\n", *count);
return 0;
}
你有很多错误,但最明显的错误是你的第二个 while(1==1)
循环解锁了互斥量但没有锁定它。所以如果你循环,你将解锁一个已经解锁的互斥量。这是一个错误。
您的第一个 while(1==1)
循环也有问题。您解锁互斥锁只是为了立即再次锁定它。您认为这样做的目的是什么?