【问题标题】:Threads - Mac vs Linux线程 - Mac 与 Linux
【发布时间】:2018-02-06 09:48:29
【问题描述】:

太棒了!

我刚刚用g++ / clang在Mac上完成了我的实现

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.7.0
Thread model: posix

并在 linux 上测试了我的代码

g++ (Debian 4.7.2-5) 4.7.2

运行一个相对简单的线程操作。什么在mac上有效,现在在linux上失败了:

terminate called after throwing an instance of 'std::system_error'
  what():  Operation not permitted

#include <cstddef>
#include <memory>
#include <stdlib.h>
#include <iostream>
#include <thread>
#include <vector>
#include <stdexcept>


std::vector<std::thread> threads;
            std::vector<std::tuple<std::size_t, std::size_t>> parts = splitRows(maxNumberThreads, elements);

            for (std::size_t threadIndex = 0; threadIndex < maxNumberThreads; threadIndex++)
            {
                threads.push_back(std::thread(compute<T>,parts[threadIndex], numbers, std::ref(*this),std::ref(other),std::ref(target)));
            }

线程函数定义为。将打印添加到compute 它根本不会跳转到函数中......知道为什么会发生这种情况吗?

template<typename T>
void compute(const std::tuple<std::size_t,std::size_t> part,
            const std::size_t numbers, 
            const MyClass<T>& m1, 
            const MyClass<T>& m2,
            MyClass<T>& target){

我正在编译

g++ -Wall main.cpp -o matrix.exe -std=c++11

但得到上述运行时错误。任何想法如何解决这一问题?我只使用标准库,没什么花哨的......

【问题讨论】:

    标签: c++ linux multithreading macos c++11


    【解决方案1】:

    您没有正确链接 pthread,请尝试以下命令,

    g++ -Wall main.cpp -o matrix.exe -pthread -std=c++11
    

    希望这会有所帮助。

    【讨论】:

    • 是的,工作。谢谢!那么为什么 linux 需要 pthread 而 mac 不需要 pthread 可以处理呢?
    猜你喜欢
    • 1970-01-01
    • 2014-02-17
    • 1970-01-01
    • 2019-10-15
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    • 2019-02-10
    相关资源
    最近更新 更多