代码之家  ›  专栏  ›  技术社区  ›  spalac24

MacOS X中C++中的线程

  •  7
  • spalac24  · 技术社区  · 12 年前

    我正在尝试在MacOSX Mavericks中使用标准C++(使用XCode安装)中的线程运行一些代码。但我有一些错误。下面是一个最小的工作示例:

    #include <thread>
    #include <iostream>
    
    void run (int x) {
        std::cout<<".";
    }
    
    int main (int argc, char const *argv[])
    {
        std::thread t(run);
    }
    

    我得到的错误是:

    minimal.cpp:10:17: error: no matching constructor for initialization of 'std::thread'
    std::thread t(run,0);
                ^ ~~~~~
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/thread:372:9: note: candidate constructor template not viable: requires single argument '__f', but 2 arguments
      were provided
    thread::thread(_Fp __f)
        ^
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/thread:261:5: note: candidate constructor not viable: requires 1 argument, but 2 were provided
    thread(const thread&);
    ^
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/thread:268:5: note: candidate constructor not viable: requires 0 arguments, but 2 were provided
    thread() _NOEXCEPT : __t_(0) {}
    ^
    1 error generated.
    

    我已经能够追踪到编译器定义的问题 _LIBCPP_HAS_NO_VARIADICS ,其定义原因是

    #if !(__has_feature(cxx_variadic_templates))
    #define _LIBCPP_HAS_NO_VARIADICS
    #endif
    

    如有任何帮助,将不胜感激。

    非常感谢。

    2 回复  |  直到 12 年前
        1
  •  23
  •   spalac24    12 年前

    幸亏 pwny PeterT ,我发现了错误。

    我只需要用 clang++ -std=c++11 minimal.cpp 它就像一个符咒。 我还需要一个 t.join() 以防止发生执行错误。

        2
  •  0
  •   roberto    7 年前

    我在运行同一个应用程序时得到了不同的std::线程行为。在xcode或仪器(评测)上,在xcode上,单线程/多线程比率为0.6,在仪器中,使用4线程阵列,

    这怎么可能?

     Xcode run:
    
    st...ok - lap: 4875 ms
    st/8...ok - lap: 1205 ms
    mt...ok - lap: 8330 ms
    st/mt ratio:**0.6**
    
    Instruments run:
    
    st...ok - lap: 2182 ms
    st/8...ok - lap: 545 ms
    mt...ok - lap: 596 ms
    st/mt ratio:**3.7**