Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9195023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:32:41+00:00 2026-06-17T21:32:41+00:00

With the new std::async in c++11, I thought I might go about trying to

  • 0

With the new std::async in c++11, I thought I might go about trying to implement an async version of OutputDebugString to free me of some of the performance downs that result from my usual heavy printing of every little detail through the usual OutputDebugString function.

So here is my original sync OutputDebugString implementation(which works):

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");
    }
}

and the following my very naiive attempt at an async version(which doesn’t work):

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");
        }
    }); 
}

And since the above doesn’t work I’m now at the point where I’m starting to think that async calling OutputDebugStringN might be better than trying to launch an async job inside the function itself as so (which works, but cumbersome):

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

So here are two questions I’d like to ask:

  1. How should I be going about implementing an async version of OutputDebugString?
  2. Should I even be trying to implement an async version of OutputDebugString?

Criticism on the above code and any other comments are also much welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:32:42+00:00Added an answer on June 17, 2026 at 9:32 pm

    I think you should have a queue for your messages instead of starting a thread with each call to your function, that way your messages will be output clean and in the right order.

    So your function e.g. OutputDebugStringN(const char *format, ... ) would create the message string and then queue the string that a seperate printout thread reads from. That thread would call OutputDebugString.

    Here’s an example – not complete though, no error handling and print_from_queue should be modified to run until some termination condition and be a bit more friendly to the 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");
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get std::async to launch a function that accepts a unique_ptr with
while looking at some code I stumbled onto: throw /*-->*/new std::exception (//... and I
If I am right, the std::async uses a new thread and calls the method
Do I understand the new Std right that shared_ptr is not required to use
Reason why somebody would be interested in //... new std::thread (func,arg1,arg2); } is that
Does anyone has experience with the rather new std::async ? We are currently implementing
I've been thinking about std::async and how one should use it in future compiler
I am having hard time digesting this syntax: void* operator new[](std::size_t, const std::nothrow_t&) throw();
In a dll, I'm doing std::vector<foo*>* v = new std::vector<foo*>(); foo* f = new
std = new Date(2012-06-22 00:05:00); std_string = std.getHours() + : + std.getMinutes(); On mobile

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.