来自stringstream的重复输出
Duplicate output from stringstream
我很抱歉,如果它是重复的,我已经在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的已知问题。只需将调试类型更改为本机即可。
为VS20101。在解决方案资源管理器中,选择项目。
2。在"视图"菜单上,单击"属性"
3。在"属性页"对话框中,展开"配置属性"节点,然后选择"调试"。
4。将调试器类型设置为本机
检查下面的链接,该票已被关闭,因为不可复制,但还有其他人面临类似的情况
http://connect.microsoft.com/VisualStudio/feedback/details/774425/outputdebugstring-prints-twice-in-the-vs-output-window-on-windows-8相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- AES加密到解密未正确输出
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- C++格式化输出问题
- 将值从二维数组输出到文本文件
- std::stringstream.str() 输出垃圾
- c++ stringstream输出性能
- 使用stringstream输出一个四舍五入的浮点数
- 无法从stringstream中解析数字并正确输出
- 来自stringstream的重复输出