c++ - Is std::async broken in gcc 4.7 on linux? -


i'm testing std::async in isolation before using in real code, verify works correctly on platform (which ubuntu 12.10 64-bit).

it works (somewhat rarely) , hangs. if works you, don't jump conclusions. try few more times, hang.

if remove pthread_mutex test, doesn't hang. smallest code can reproduce hang. there reason can't mix c pthread code c++ async code?

#include <iostream> #include <pthread.h> #include <chrono> #include <future> #include <iomanip> #include <sstream> #include <type_traits>  template<typename t> std::string format_ns(t &&value) {     std::stringstream s;     if (std::is_floating_point<t>::value)         s << std::setprecision(3);      if (value >= 1000000000)         s << value / 1000000000 << "s";     else if (value >= 1000000)         s << value / 1000000 << "ms";     else if (value >= 1000)         s << value / 1000 << "us";     else         s << value  << "ns";     return s.str(); }  template<typename f> void test(const std::string &msg, int iter, f &&lambda) {     std::chrono::high_resolution_clock clock;     auto st = clock.now();      int i;     (i = 0; < iter; ++i)         lambda();     auto en = clock.now();     std::chrono::nanoseconds dur = std::chrono::duration_cast<         std::chrono::nanoseconds>(en-st);      std::cout << msg << format_ns(dur.count() / i) << std::endl; }  int test_pthread_mutex() {     pthread_mutex_t m = pthread_mutex_initializer;      test("pthread_mutex_lock/pthread_mutex_unlock: ", 1000000000,     [&]()     {         pthread_mutex_lock(&m);         pthread_mutex_unlock(&m);     });      pthread_mutex_destroy(&m);      return 0; }  int test_async() {     test("async: ", 100,     [&]()     {         auto asy = std::async(std::launch::async, [](){});         asy.get();     });      return 0; }  int main() {     test_pthread_mutex();     test_async(); } 

here build command line:

g++ -wextra -wall  --std=c++11 -pthread mutexperf/main.cpp 

there no build output messages.

here output of g++ -v

using built-in specs. collect_gcc=g++ collect_lto_wrapper=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper target: x86_64-linux-gnu configured with: ../src/configure -v --with-pkgversion='ubuntu/linaro 4.7.2-2ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.7/readme.bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu thread model: posix gcc version 4.7.2 (ubuntu/linaro 4.7.2-2ubuntu1) 

i tried on few different computers , found indeed worked fine @johan commented. investigated machine using , found evidence hard drive beginning fail. has bad sectors , saw dmesg report several "hard resets" of hdd after unusual 4 second freeze. odd, hadn't seen issues before posted question. it's subtle/intermittent corruption when compiling/linking or perhaps when loading executable.

[44242.380936] ata3.00: exception emask 0x10 sact 0x0 serr 0x800000 action 0x6 frozen [44242.380942] ata3.00: irq_stat 0x08000000, interface fatal error [44242.380946] ata3: serror: { linkseq } [44242.380950] sr 2:0:0:0: cdb:  [44242.380952] event status notification: 4a 01 00 00 10 00 00 00 08 00 [44242.380965] ata3.00: cmd a0/00:00:00:08:00/00:00:00:00:00/a0 tag 0 pio 16392 in [44242.380965]          res 50/00:03:00:08:00/00:00:00:00:00/a0 emask 0x10 (ata bus error) [44242.380968] ata3.00: status: { drdy } [44242.380974] ata3: hard resetting link [44242.700025] ata3: sata link 1.5 gbps (sstatus 113 scontrol 300) [44242.704849] ata3.00: configured udma/100 [44242.720055] ata3: eh complete [44970.117542] ata3.00: exception emask 0x10 sact 0x0 serr 0x800100 action 0x6 frozen [44970.117547] ata3.00: irq_stat 0x08000000, interface fatal error [44970.117551] ata3: serror: { unrecovdata linkseq } [44970.117555] sr 2:0:0:0: cdb:  [44970.117557] event status notification: 4a 01 00 00 10 00 00 00 08 00 [44970.117570] ata3.00: cmd a0/00:00:00:08:00/00:00:00:00:00/a0 tag 0 pio 16392 in [44970.117570]          res 50/00:03:00:08:00/00:00:00:00:00/a0 emask 0x10 (ata bus error) [44970.117573] ata3.00: status: { drdy } [44970.117579] ata3: hard resetting link [44970.436662] ata3: sata link 1.5 gbps (sstatus 113 scontrol 300) [44970.443159] ata3.00: configured udma/100 [44970.456639] ata3: eh complete 

thanks spent time looking @ issue!


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -