2017-08-29 68 views
3

太棒了!主題 - Mac vs Linux

我剛剛完成我的Mac上實現與g++/clang

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 

編譯但得到上述運行時錯誤。任何想法如何解決這一問題?我只用STD庫,沒有什麼花哨......

回答

2

你沒有正確連接並行線程,試試下面的命令,

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

希望這有助於。

+1

是的,工作。謝謝!那麼,爲什麼linux需要pthread而mac可以處理沒有? –