2013-04-05 69 views
5

我在测试std::async之前在真正的代码中使用它,以验证它在我的平台(这是64位的Ubuntu 12.10)上正常工作。在Linux上的gcc 4.7中std :: async被破坏了吗?

它的工作原理(很少),通常只是挂起。如果它适合你,不要妄下结论。多试几次,它可能会挂起。

如果我删除了pthread_mutex测试,它不会挂起。这是我可以用来重现挂起的最小代码。有没有理由不能将C pthread代码与C++异步代码混合?

#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; 
    for (i = 0; i < 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(); 
} 

这里是构建命令行:

g++ -Wextra -Wall --std=c++11 -pthread mutexperf/main.cpp 

没有构建输出消息。

这里的输出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) 
+1

我和你有相同的编译器和设置。我无法复制。代码看起来不错。 – 2013-04-06 13:13:08

+0

@johan我认为它可能不会重现。我会在其他一些机器上尝试一下,看看会发生什么。谢谢。 – doug65536 2013-04-06 21:34:36

回答

2

我在几台不同的电脑上试了一下,发现它确实很像@johan评论的。我调查了我正在使用的机器,发现有证据表明硬盘驱动器开始出现故障。它有一些坏扇区,并且看到dmesg在不寻常的4秒钟冻结之后报告硬盘的几个“硬重置”。奇怪的是,在我发布这个问题之前,我还没有看到任何问题。编译/链接或加载可执行文件时可能会出现细微的/间歇性的损坏。

[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] Get 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 up 1.5 Gbps (SStatus 113 SControl 300) 
[44242.704849] ata3.00: configured for 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] Get 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 up 1.5 Gbps (SStatus 113 SControl 300) 
[44970.443159] ata3.00: configured for UDMA/100 
[44970.456639] ata3: EH complete 

感谢任何花时间看我的问题的人!

0

你有没有尝试复制互斥而不是由拉姆达引用传递?

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; 
} 
相关问题