gpt4 book ai didi

C++ 异步 OutputDebugString

转载 作者:太空狗 更新时间:2023-10-29 19:46:18 30 4
gpt4 key购买 nike

使用 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());} );

所以我想问两个问题:

  1. 我应该如何实现 OutputDebugString 的异步版本?
  2. 我是否应该尝试实现 OutputDebugString 的异步版本?

也欢迎对上述代码提出批评和任何其他意见。

最佳答案

我认为您应该为您的消息创建一个队列,而不是在每次调用您的函数时启动一个线程,这样您的消息将以正确的顺序干净地输出。

所以你的功能例如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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com