- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
使用 c++11 中的新 std::async,我想我可能会尝试实现 OutputDebugString 的异步版本,以使我摆脱一些由于我通常大量打印每个小细节而导致的性能下降通过通常的 OutputDebugString 函数。
所以这是我最初的同步 OutputDebugString 实现(有效):
static void OutputDebugStringN(const char *format, ...)
{
char outstring[256];
memset(outstring, 0, sizeof(outstring));
try
{
va_list args = {0};
va_start(args, format); //args = (va_list) (&format+1);
vsprintf_s(outstring, format, args);
va_end(args);
OutputDebugString(outstring);
}
catch (...) //most likely reference val arg error (va_list doesn't support ref args)
{
OutputDebugString("[OutputDebugStringN] Something went wrong\n");
}
}
以及以下我对异步版本的非常幼稚的尝试(不起作用):
static void OutputDebugStringN(const char *format, ...)
{
auto future = std::async([]{
char outstring[256];
memset(outstring, 0, sizeof(outstring));
try
{
va_list args = {0};
va_start(args, format); //args = (va_list) (&format+1);
vsprintf_s(outstring, format, args);
va_end(args);
OutputDebugString(outstring);
}
catch (...) //most likely reference val arg error (va_list doesn't support ref args)
{
OutputDebugString("[OutputDebugStringN] Something went wrong\n");
}
});
}
由于上述方法不起作用,我现在开始认为异步调用 OutputDebugStringN 可能比尝试在函数本身内部启动异步作业更好(这可行,但是麻烦):
auto dstring = std::async([]{ OutputDebugStringN("[NovelScript::ParseTokens] searched bookmark: \"%s\" does not exist\n", bookmark.c_str());} );
所以我想问两个问题:
也欢迎对上述代码提出批评和任何其他意见。
最佳答案
我认为您应该为您的消息创建一个队列,而不是在每次调用您的函数时启动一个线程,这样您的消息将以正确的顺序干净地输出。
所以你的功能例如OutputDebugStringN(const char *format, ... )
将创建消息字符串,然后将单独的打印输出线程从中读取的字符串排队。该线程将调用 OutputDebugString
。
这是一个示例 - 虽然不完整,但不应修改错误处理和 print_from_queue 以运行直到出现某些终止条件并且对 CPU 更友好一些。
std::mutex g_m;
std::deque<std::string> que;
std::atomic<bool> endcond = false;
void queue(std::string msg)
{
std::lock_guard<mutex> _(g_m);
que.push_back(msg);
}
void print_from_queue()
{
while ( !endcond )
{
if ( que.size() )
{
std::lock_guard<mutex> _(g_m);
std::string msg = que.front();
que.pop_front();
OutputDebugStringA(msg.c_str());
}
}
}
int debugf( const char *format,... )
{
std::vector<char> line(256);
va_list args;
va_start( args, format );
int len = vsprintf_s( &line[0], line.size(), format, args );
va_end( args );
queue( &line[0] );
return len;
}
int _tmain(int argc, _TCHAR* argv[])
{
auto thr = std::async( std::launch::async, print_from_queue );
debugf("message1");
debugf("message2");
...
关于C++ 异步 OutputDebugString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14536402/
这应该输出到输出窗口。但是,它没有,我不确定为什么。当我在 Debug模式下运行它时,buffer = "*** NUM ADAPTERS = 2",但它没有显示在输出窗口中。 UINT i = 0;
我使用: OutputDebugString(L"My error"); 在 Visual Studio 2010 中,我没有显示“我的错误”,而是在窗口中得到了一个“错误”。 我该如何解决这个问题?
我有一个问题,由于神秘的原因出现又消失。不久前,当我开始这个项目时,我发现了一个相当方便的函数,它允许在 VS2010 中输出调试窗口。有一段时间效果很好。 现在它显示错误不一致。这意味着有时代码会编
我正在使用使用函数 OutputDebugString() 的第三方库,并且在阅读 MSDN 文档时,它似乎表明这是为了打印到调试器。 但这对我来说很不方便,如果没有连接调试器,有没有办法读取这个输出
我正在 try catch 所有 OutputDebugString使用以下代码的消息(包括来自服务的消息)。在我迁移到 Windows 7 之前它运行良好。 问题在于,由于 Windows Vist
使用 c++11 中的新 std::async,我想我可能会尝试实现 OutputDebugString 的异步版本,以使我摆脱一些由于我通常大量打印每个小细节而导致的性能下降通过通常的 Output
通常,当我需要在 Windows 中进行调试输出时,我会使用以下 C 代码段: #ifdef _DEBUG #define DBGPRINT( kwszDebugFormatString, ... )
我最近不得不对一些 Delphi 代码进行一些更改。因此,我有一些基本问题: 一般情况下如何输出到控制台? 如何使用字符串变量 fx 输出到控制台? 我开始使用 OutputDebugString,但
我尝试在 MSVC 2013 中调试 C 中的一些值,并决定第一次使用此函数。我不知道,但我只收到 “???????????????” 在输出窗口中。我检查了调试类型,尝试将两者都设置为“自动/混合”
我使用 OutputDebugString 转储\t 分隔数据,然后使用 ex-Sysinternals DebugView 捕获它。 问题是 DebugView 中的所有数据似乎都是以空格分隔的,因
我正在编写一个 C++ 应用程序,我想向 OutputDebugString() 添加一个时间戳。我已经知道如果我使用 DebugView 观看应用程序,它会自动显示时间戳。但出于某种特殊原因,我想将
我创建了一个 Win32 应用程序并且有一个登录表单,但我无法获得编辑窗口值。我是这样做的,但什么也没得到。我的错误在哪里? #define passwordWindowId 2 HWND passw
在发布构建中调用 OutputDebugString 是否有显着的开销? 最佳答案 测量 - 1000 万个调用大约需要 50 秒。我认为对于未使用的功能来说这是一个很大的开销。 使用宏可以帮助在发布
我在 Delphi 代码中使用 OutputDebugString,但收到错误: Error: Undeclared identifier: 'OutputDebugString' 这个OutputD
我正在尝试使用 WDK 构建环境构建 C++ 文件,并尝试在 DebugView 工具中查看输出。所以,当我尝试 windows 方法 OutputDebugString(L"Inside the m
我正在研究将现有的 Windows MFC 控件移植到 OS X/Carbon 的可行性。我的测试平台是使用 XCode 3 向导生成的 C++ Carbon 应用程序。 我正在寻找一种将一些跟踪信息
我想知道是否可以绕过 OutputDebugString?我希望 OutputDebugString 输出显示在 DebugView 中,而不是显示在内部 Delphi 事件查看器窗口中。但我找不到一
我的应用程序中有一些“流氓”OutputDebugString 调用,它打印出“T”,但我无法找到它。 是否可以以某种方式在 OutputDebugString 函数上设置断点并查看它是从哪里调用的?
我的 WPF 应用程序使用第三方 Win32 dll,通过 OutputDebugString 记录消息。 我可以在 Visual Studio 中或通过 DebugView 查看 OutputDeb
我在网上搜索了一段时间如何使用 OutputDebugString() 输出整数或可选的 float 。如果我可以使用此命令将我的调试数据从我的外部可执行文件直接写入控制台,那会更容易。但是我只让它与
我是一名优秀的程序员,十分优秀!