热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用互斥时,“printf”输出两次(或更多?我不确定)?

我正在学习线程同步.我的测试代码如下:#include#includepthread_cond_tcondPTHREAD_COND_INIT

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

#include


#include
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”会做什么?为什么输出两次.

解决方法:

一旦互斥锁被破坏,所有赌注都将被取消.在确定没有线程可以使用它之前,不应销毁互斥锁​​.


推荐阅读
author-avatar
韩尕猫_345
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有