C ++多线程文本输出打印到Dos控制台重叠/缺少输出 [英] C++ Multi-thread text output print to dos console overlapped/Mess up the outputs

查看:109
本文介绍了C ++多线程文本输出打印到Dos控制台重叠/缺少输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从dos提示符执行的多线程程序,使用std :: cout有一些打印和输出转储到dos控制台,但是当thread1和thread2完成工作后,然后join()到主应用程序,一些打印输出和输出重叠且未对齐(没有换行符,互相碰到).

I had a multi-thread program which execute from dos prompt, there are some prints and outputs dump to dos console using std::cout, but when thread1 and thread2 finished it jobs and then join() to main app, some printouts and outputs were overlapped and not aligned (no newline,running into each other).

有时候还可以.如果您有什么建议,非常感谢您的帮助.

Sometime they are ok. If you have some advises, I am really appreciate for your helps.

安德鲁

推荐答案

好,很简单.输出没有并发控制,您正在争夺数据.您需要让线程在使用输出之前锁定互斥量,然后在完成输出后将其释放.

Well, it's simple. Output has no concurrency control, you're getting data races. You need to have your threads lock a mutex before they use output, then release it when they're done.

这篇关于C ++多线程文本输出打印到Dos控制台重叠/缺少输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆