Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c++ > #638

semantics of multithreading?

From siegebell@googlemail.com
Newsgroups comp.std.c++
Subject semantics of multithreading?
Date 2013-06-10 12:23 -0700
Organization unknown
Message-ID <b5950868-7d0a-4485-8c1e-166e74843669@googlegroups.com> (permalink)

Show all headers | View raw


Hello,

I have a question about the semantics of multithreading in C++.

Consider a program that forks two threads, each of which atomically sets the value of the same variable to different values, and then joins the threads and prints the value of the variable. Depending on the outcome of the thread interleaving, a different value may be printed.

// pseudo code!
atomic_int x;
void f1() { x.store(1); }
void f2() { x.store(2); }

int main(...)
{
   p1 = fork(f1);
   p2 = fork(f2);
   p1.join();
   p2.join();
   cout << x.load(); // x may be either 1 or 2
   return 0;
}


Does the C++11 standard explicitly state whether the compiler is required to preserve all thread interleavings that may result in different outputs? Or can it be treated as "unspecified behavior", allowing the compiler to choose fewer interleavings?

I am suspicious of the latter case because it could allow the compiler to introduce a deadlock or livelock in some programs. Also, I associate "unspecified behavior" with choices that the compiler may make, but if the choice has an stateful effect, then the program is undefined.

If choosing a particular thread interleaving does not result in a different output (or "observable behavior"), may the compiler do so? Does the standard allow new threads to be introduced, e.g. to automatically parallelize a program?


Thanks for your time & help,
-cj


-- 
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]

Back to comp.std.c++ | Previous | NextNext in thread | Find similar


Thread

semantics of multithreading? siegebell@googlemail.com - 2013-06-10 12:23 -0700
  Re: semantics of multithreading? Jason McKesson <jmckesson@googlemail.com> - 2013-06-11 12:19 -0700
  Re: semantics of multithreading? Martin Bonner <martinfrompi@yahoo.co.uk> - 2013-06-13 08:42 -0700
    Re: semantics of multithreading? siegebell@googlemail.com - 2013-06-15 08:42 -0600
      Re: semantics of multithreading? Jason McKesson <jmckesson@googlemail.com> - 2013-06-16 19:58 -0600
        Re: semantics of multithreading? usenet@mkarcher.dialup.fu-berlin.de (Michael Karcher) - 2013-06-18 11:36 -0700
          Re: semantics of multithreading? siegebell@googlemail.com - 2013-06-23 19:19 -0600
        Re: semantics of multithreading? siegebell@googlemail.com - 2013-06-23 19:18 -0600

csiph-web