gpt4 book ai didi

c - 使用互斥锁时, "printf"输出两次(或更多?我不确定)?

转载 作者:可可西里 更新时间:2023-11-01 11:45:43 27 4
gpt4 key购买 nike

我正在学习线程同步。我的测试代码如下:

#include <pthread.h>
#include <stdio.h>

pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int count = 0;
void* add(void * params)
{
while(1)
{
pthread_mutex_lock(&mutex);
fprintf(stdout, "thread:%ld, count:%d\n",pthread_self(), count);
count++;
pthread_mutex_unlock(&mutex);
}
return 0;
}

void* print(void * params)
{
while(1)
{
pthread_mutex_lock(&mutex);
if (count > 100)
{
printf("count greater than 100,count: %d\n", count);
pthread_mutex_unlock(&mutex);
break;
}
pthread_mutex_unlock(&mutex);
}
return 0;
}

int main(void)
{
pthread_t thread1, thread2, thread3;
pthread_mutex_init(&mutex, NULL);
pthread_create(&thread1, NULL, add, NULL);
pthread_create(&thread2, NULL, add, NULL);
pthread_create(&thread3, NULL, print, NULL);
pthread_join(thread3, NULL);
pthread_mutex_destroy(&mutex);

return 0;
}

我以为“计数”的每个输出都会依次增加一个。然而,事实却大不相同。就像:

//program output begin: thread:139663694870272, count:0 thread:139663694870272, count:1 thread:139663694870272, count:2 thread:139663694870272, count:3 thread:139663694870272, count:4 thread:139663694870272, count:5 thread:139663686477568, count:6 thread:139663686477568, count:7 thread:139663686477568, count:8 thread:139663686477568, count:9 thread:139663686477568, count:10 thread:139663686477568, count:11 thread:139663686477568, count:12 thread:139663686477568, count:13 thread:139663686477568, count:14 thread:139663686477568, count:15 thread:139663686477568, count:16 thread:139663686477568, count:17 thread:139663686477568, count:18 thread:139663686477568, count:19 thread:139663686477568, count:20 thread:139663686477568, count:21 thread:139663686477568, count:22 (...............some lines were ignored)thread:139663686477568, count:172 count greater than 100,count: 173 thread:139663686477568, count:173 thread:139663686477568, count:174 thread:139663686477568, count:175 thread:139663686477568, count:176 thread:139663686477568, count:177 thread:139663686477568, count:178 thread:139663686477568, count:179 thread:139663686477568, count:180 thread:139663686477568, count:181 thread:139663686477568, count:182 thread:139663686477568, count:183 thread:139663686477568, count:184 thread:139663686477568, count:185 thread:139663686477568, count:186 thread:139663686477568, count:187 thread:139663686477568, count:188 thread:139663686477568, count:189 thread:139663686477568, count:190 thread:139663686477568, count:191 thread:139663686477568, count:192 thread:139663686477568, count:193 thread:139663686477568, count:194 thread:139663686477568, count:195 thread:139663686477568, count:196 thread:139663686477568, count:197 thread:139663686477568, count:198 thread:139663686477568, count:199 thread:139663686477568, count:200 thread:139663686477568, count:201 thread:139663686477568, count:202 thread:139663686477568, count:203 thread:139663686477568, count:204 thread:139663686477568, count:205 thread:139663686477568, count:206 thread:139663686477568, count:206 thread:139663686477568, count:207 thread:139663686477568, count:208 thread:139663686477568, count:209 thread:139663686477568, count:210 thread:139663686477568, count:210 thread:139663686477568, count:211

我不知道“printf”在这种情况下做了什么?为什么输出两次。

最佳答案

一旦互斥锁被销毁,所有的赌注都将落空。在确定没有线程可以使用互斥量之前,不应销毁它。

关于c - 使用互斥锁时, "printf"输出两次(或更多?我不确定)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32076679/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com