Reply to comment

The short answer is because

The short answer is because we are updating m_fibonacci_values inside of a mutex locked critical section. Technically speaking volatile alerts the compiler that data may change in "unexpected" ways. Because of the mutex lock, every change to m_fibonacci_values is controlled and it is impossible for one thread to update it while another is trying to read it. As an optimization (and due to the fact that booleans are updated in a single instruction inside the CPU) we are able to update m_running and m_stop_requested without using a mutex, a consequence of that is that we need to mark it as volatile.

So to sum up:

  • If the variable you are using is a simple data type (ie int, bool) you do not need a mutex. If you do not use a mutex you must use volatile.
  • If the variable is complex (ie std::string, std::vector) you must use a mutex if there is any possibility at all that the variable will be updated while another thread is trying to access it.

I'm sure there is a much deeper explanation of this that is outside of my scope of knowledge.

Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • Lines and paragraphs break automatically.
  • You may post PHP code. You should include <?php ?> tags.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Images can be added to this post.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.