https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62036
--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> --- As pointed out at http://stackoverflow.com/questions/25115189/optimization-bug-regarding-gcc-stdthread#comment39095137_25115189 (where you apparently copied this code from) there's a known bug which is fixed in GCC 4.9, see PR51253