Note that there are some explanatory texts on larger screens.

plurals
  1. POIs 'volatile' needed in this multi-threaded C++ code?
    primarykey
    data
    text
    <p>I've written a Windows program in C++ which at times uses two threads: one background thread for performing time-consuming work; and another thread for managing the graphical interface. This way the program is still responsive to the user, which is needed to be able to abort a certain operation. The threads communicate via a shared <code>bool</code> variable, which is set to <code>true</code> when the GUI thread signals the worker thread to abort. Here is the code which implements this behaviour (I've stripped away irrelevant parts):</p> <h1>CODE EXECUTED BY THE GUI THREAD</h1> <pre><code> class ProgressBarDialog : protected Dialog { /** * This points to the variable which the worker thread reads to check if it * should abort or not. */ bool volatile* threadParameterAbort_; ... BOOL CALLBACK ProgressBarDialog::DialogProc( HWND dialog, UINT message, WPARAM wParam, LPARAM lParam ) { switch( message ) { case WM_COMMAND : switch ( LOWORD( wParam ) ) { ... case IDCANCEL : case IDC_BUTTON_CANCEL : switch ( progressMode_ ) { if ( confirmAbort() ) { // This causes the worker thread to be aborted *threadParameterAbort_ = true; } break; } return TRUE; } } return FALSE; } ... }; </code></pre> <h1>CODE EXECUTED BY THE WORKER THREAD</h1> <pre><code> class CsvFileHandler { /** * This points to the variable which is set by the GUI thread when this * thread should abort its execution. */ bool volatile* threadParamAbort_; ... ParseResult parseFile( ItemList* list ) { ParseResult result; ... while ( readLine( &line ) ) { if ( ( threadParamAbort_ != NULL ) && *threadParamAbort_ ) { break; } ... } return result; } ... }; </code></pre> <p><code>threadParameterAbort_</code> in both threads point to a <code>bool</code> variable declared in a structure which is passed to the worker thread upon creation. It is declared as</p> <pre><code>bool volatile abortExecution_;</code></pre> <p><b>My question is:</b> do I need to use <code>volatile</code> here, and is the code above sufficient to ensure that the program is thread-safe? The way I've reasoned for justifying the use of <code>volatile</code> here (see <a href="https://stackoverflow.com/questions/3604569/what-kinds-of-optimizations-does-volatile-prevent-in-c">this question</a> for background) is that it will:</p> <ul> <li><p>prevent the reading of <code>*threadParameterAbort_</code> to use the cache and instead get the value from memory, and</p></li> <li><p>prevent the compiler from removing the <code>if</code> clause in the worker thread due to optimization.</p></li> </ul> <p>(The following paragraph is only concerned with the thread-safety of the program as such and <em>does not</em>, I repeat, <em>does not</em> involve claiming that <code>volatile</code> in any way provides any means of ensuring thread-safety.) As far as I can tell, it should be thread-safe as setting of a <code>bool</code> variable should in most, if not all, architectures be an atomic operation. But I could be wrong. And I'm also worried about if the compiler may reorder instructions such as to break thread-safety. But better be safe (no pun intended) than sorry.</p> <p><b>EDIT:</b> A minor mistake in my wording made the question appear as if I was asking if <code>volatile</code> is enough to ensure thread-safety. This was not my intent -- <code>volatile</code> does indeed not ensure thread-safety in any way -- but what I meant to ask was if the code provided above exhibit the correct behaviour to ensure that the program is thread-safe.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

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