Note that there are some explanatory texts on larger screens.

plurals
  1. POC++11 static local variables and threads
    text
    copied!<p>Is static local variables safe for a class that creates/uses <code>std::thread</code>s?</p> <p>Because when I use something like this:</p> <pre><code>logger&amp; logger::get_instance(void) { static logger lg; return lg; } </code></pre> <p>And try to exit (force close) the executable, it crashes/exits improperly (somethings the Visual Studio 2012 debugger even crashes).</p> <p>When I don't do that, the program exits gracefully when i force close.</p> <p>Here's the stack call when it crashes</p> <pre><code> ntdll.dll!77c10dbd() Unknown [Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll] ntdll.dll!77b7bfdc() Unknown kernel32.dll!75b55bab() Unknown &gt; msvcr110d.dll!__crtCreateThreadpoolWait(void (_TP_CALLBACK_INSTANCE *, void *, _TP_WAIT *, unsigned long) * pfnwa, void * pv, _TP_CALLBACK_ENVIRON_V1 * pcbe) Line 569 C msvcr110d.dll!Concurrency::details::RegisterAsyncWaitAndLoadLibrary(void * waitingEvent, void (_TP_CALLBACK_INSTANCE *, void *, _TP_WAIT *, unsigned long) * callback, void * data) Line 675 C++ msvcr110d.dll!Concurrency::details::ExternalContextBase::PrepareForUse(bool explicitAttach) Line 120 C++ msvcr110d.dll!Concurrency::details::ExternalContextBase::ExternalContextBase(Concurrency::details::SchedulerBase * pScheduler, bool explicitAttach) Line 52 C++ msvcr110d.dll!Concurrency::details::SchedulerBase::GetExternalContext(bool explicitAttach) Line 1579 C++ msvcr110d.dll!Concurrency::details::SchedulerBase::AttachExternalContext(bool explicitAttach) Line 1527 C++ msvcr110d.dll!Concurrency::details::SchedulerBase::CreateContextFromDefaultScheduler() Line 569 C++ msvcr110d.dll!Concurrency::details::SchedulerBase::CurrentContext() Line 402 C++ msvcr110d.dll!Concurrency::details::LockQueueNode::LockQueueNode(unsigned int timeout) Line 616 C++ msvcr110d.dll!Concurrency::critical_section::lock() Line 1017 C++ msvcp110d.dll!mtx_do_lock(_Mtx_internal_imp_t * * mtx, const xtime * target) Line 65 C++ msvcp110d.dll!_Mtx_lock(_Mtx_internal_imp_t * * mtx) Line 144 C++ escobar.exe!std::_Mtx_lockX(_Mtx_internal_imp_t * * _Mtx) Line 68 C++ escobar.exe!std::_Mutex_base::lock() Line 43 C++ escobar.exe!std::unique_lock&lt;std::mutex&gt;::unique_lock&lt;std::mutex&gt;(std::mutex &amp; _Mtx) Line 228 C++ escobar.exe!escobar::utilities::blocking_queue&lt;escobar::logging::log_message *&gt;::interrupt() Line 71 C++ escobar.exe!escobar::logging::log_worker::~log_worker() Line 17 C++ escobar.exe!escobar::logging::log_worker::`scalar deleting destructor'(unsigned int) C++ escobar.exe!escobar::logging::logger::close() Line 72 C++ escobar.exe!escobar::logging::logger::~logger() Line 27 C++ escobar.exe!`escobar::logging::logger::get_instance'::`2'::`dynamic atexit destructor for 'lg''() C++ msvcr110d.dll!doexit(int code, int quick, int retcaller) Line 585 C msvcr110d.dll!_cexit() Line 410 C msvcr110d.dll!__CRTDLL_INIT(void * hDllHandle, unsigned long dwReason, void * lpreserved) Line 296 C msvcr110d.dll!_CRTDLL_INIT(void * hDllHandle, unsigned long dwReason, void * lpreserved) Line 210 C ntdll.dll!77bb2846() Unknown ntdll.dll!77bb2893() Unknown ntdll.dll!77bc09c8() Unknown ntdll.dll!77bc08ad() Unknown KernelBase.dll!75525bbb() Unknown KernelBase.dll!75525c51() Unknown kernel32.dll!75b58543() Unknown ntdll.dll!77bbac69() Unknown ntdll.dll!77bbac3c() Unknown </code></pre> <p>Here are a couple of the functoin</p> <pre><code>log_worker::~log_worker(void) { this-&gt;queue.interrupt(); service.join(); } void log_worker::run(void) { while (true) { log_message* msg; if (this-&gt;queue.dequeue(msg) == false) break; this-&gt;lg-&gt;print_log_message(msg); delete msg; } } bool dequeue(T&amp; item) { std::unique_lock&lt;std::mutex&gt; lock(m); // handles spurious wakeups while (!this-&gt;data_available &amp;&amp; !this-&gt;interrupted) cv.wait(lock); if (this-&gt;interrupted) return false; item = std::move(this-&gt;front()); this-&gt;pop(); if (this-&gt;empty()) this-&gt;data_available = false; return true; } void interrupt(void) { std::unique_lock&lt;std::mutex&gt; lock(m); this-&gt;interrupted = true; cv.notify_all(); printf("notified threads...\n"); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload