加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C异步OutputDebugString

发布时间:2020-12-16 10:23:46 所属栏目:百科 来源:网络整理
导读:使用c 11中的新std :: async,我想我可能会尝试实现OutputDebugString的异步版本,以免我通过通常的OutputDebugString函数通常对每个小细节进行大量打印而导致性能降低. . 所以这是我原来的同步OutputDebugString实现(可行): static void OutputDebugStringN(
使用c 11中的新std :: async,我想我可能会尝试实现OutputDebugString的异步版本,以免我通过通常的OutputDebugString函数通常对每个小细节进行大量打印而导致性能降低. .

所以这是我原来的同步OutputDebugString实现(可行):

static void OutputDebugStringN(const char *format,...)
{
    char outstring[256];
    memset(outstring,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 wrongn");
    }
}

以及我对非同步版本的非常小的尝试(不起作用):

static void OutputDebugStringN(const char *format,...)
{
    auto future = std::async([]{
        char outstring[256];
        memset(outstring,sizeof(outstring));
        try
        {
            va_list args = {0};
            va_start(args,format); //args = (va_list) (&format+1);

            vsprintf_s(outstring,args);

            va_end(args);

            OutputDebugString(outstring);
        }
        catch (...) //most likely reference val arg error (va_list doesn't support ref args)
        {
            OutputDebugString("[OutputDebugStringN] Something went wrongn");
        }
    }); 
}

由于上述方法不起作用,我现在开始认为异步调用OutputDebugStringN可能比尝试在函数内部启动异步作业更好(这很有用,但很麻烦):

auto dstring = std::async([]{ OutputDebugStringN("[NovelScript::ParseTokens] searched bookmark: "%s" does not existn",bookmark.c_str());} );

所以这里有两个我想问的问题:

>我应该如何实现OutputDebugString的异步版本?
>我是否应该尝试实现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(),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");
...

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读