Friday, 15 July 2011

c++ - C++11 thread vs boost thread -



c++ - C++11 thread vs boost thread -

am in process of migrating project c++11 standard msvc110, unfortunately thread variable, used on dll, behaving different boost version had.

so, working on msvc90, dll calls initdll thread created. thread served listener along main thread of dll. when create thread hangs , nothing, not executing function used initialize thread.

could help me explaining how can same behavior boost version?

edit: code

sorry, couldn't reply code on comments

an application uses logger through dll. utilize logger in simple console application goes this

#include <somewhere/logger.h> int main() { cool_logger("here go logging on console!"); homecoming 0; }

we can discuss way code written (taken demos mentioned), how initialized dll , thread is:

#include "logger.h" #ifdef _win32 bool apientry dllmain( hmodule hmodule, dword ul_reason_for_call, lpvoid lpreserved ) { switch (ul_reason_for_call) { case dll_process_attach: thelog::initlog(); break; case dll_thread_attach: break; case dll_thread_detach: break; case dll_process_detach: break; } homecoming true; } #endif #include <thread> void initlog() { // init taken library demos std::thread m_thread(loglistener); } void loglistener() { while(!bappend) { std::cin>>str; // alter log behavior according user input } } // stop thread when shutting downwards void endlog() { // retrieve thread thought id or other way thread.join(); }

if things go wrong in dllmain severely limited in can --- windows loader terminate app, , error handlers not called.

a hang suggests code doing requires loading dll, or waiting dll initialize, neither of can happen until phone call dllmain dll finished. possible implementation of std::thread doing 1 of these things.

edit: 1 way avoid problem utilize std::call_once in every exported function communicates background thread, ensure thread started. way not using std::thread in dllmain, don't need expose "init" function.

c++ multithreading visual-c++ boost c++11

No comments:

Post a Comment