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:
- How should I be going about implementing an async version of OutputDebugString?
- 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.