来自stringstream的重复输出

Duplicate output from stringstream

本文关键字:输出 stringstream 来自      更新时间:2023-10-16

我很抱歉,如果它是重复的,我已经在SO中搜索过了,我已经看到有类似的问题,但我仍然无法调试这个问题。

我使用stringstream只是为了简单的调试。我有这个宏:

#else 
#include <sstream>
extern std::wstringstream trc;
#define DEBUG_MSG(x) 
    trc.str(std::wstring());
    trc<<x;
    OutputDebugString(trc.str().c_str())
#endif 

当我使用

DEBUG_MSG("IPCFacilities: InsertCtrlMessage: write." <<" Time: "<<GetTickCount64()<<std::endl);

在DebugView中我得到:

IPCFacilities: InsertCtrlMessage: write. Time: 265793562
IPCFacilities: InsertCtrlMessage: write. Time: 265793562

(输出输出两次)

我做错了什么?

这只是一个竞争条件的问题。

如果代码像这样交错:

thread 1: trc.str(std::wstring());
thread 2: trc.str(std::wstring());
thread 1: trc<<x;
thread 1: OutputDebugString(trc.str().c_str());
thread 2: trc<<x;
thread 2: OutputDebugString(trc.str().c_str());

输出似乎打印了两次,但实际上不是。这只是我代码中的一个bug。宏观总是不利于这类事情,这次我吸取了教训。谢谢大家的参与

我记得我遇到过类似的问题,这是OutputDebugString的已知问题。只需将调试类型更改为本机即可。

为VS2010

1。在解决方案资源管理器中,选择项目。

2。在"视图"菜单上,单击"属性"

3。在"属性页"对话框中,展开"配置属性"节点,然后选择"调试"。

4。将调试器类型设置为本机

检查下面的链接,该票已被关闭,因为不可复制,但还有其他人面临类似的情况

http://connect.microsoft.com/VisualStudio/feedback/details/774425/outputdebugstring-prints-twice-in-the-vs-output-window-on-windows-8