2013-03-30 148 views
11

所以我一直試圖通過使用MinGW編譯器來獲得下面的代碼來編譯和運行在Windows上。MinGW和std :: thread

#include <iostream> 
#include <thread> 

void test() 
{ 
    std::cout << "test" << std::endl; 
} 

int main() 
{ 
    std::thread t(test); 
} 

我用下面的命令編譯:

g++ -std=c++11 test.cpp -o test.exe 

現在的問題是MinGW的一個應該使用的版本,我已經試過所有我知道的版本。

  1. MinGW的-構建:thread-win32
  2. MinGW的-構建:thread-posix
  3. 的MinGW-W64:stdthread experimental rubenvb
  4. 的MinGW-W64:stdthread experimental rubenvb 4.7

1號不起作用,因爲GCC apparently only supports pthread內部的東西。

2號並編譯和它基本上是均衡輸出test(見輸出的最後一行),但它也與錯誤崩潰:

terminate called without an active exception 

This application has requested the Runtime to terminate it in an unusual way. 
Please contact the application's support team for more information. 
test 

3號和4再做一次編譯,但他們不「T輸出test,而是瞬間崩潰,但有一個更具描述性的輸出:

terminate called after throwing an instance of 'std::system_error' 
    what(): Enable multithreading to use std::thread: Operation not permitted 

This application has requested the Runtime to terminate it in an unusual way. 
Please contact the application's support team for more information. 

谷歌給我帶來的當然就是GCC bug tracker和其他一些職位,那建議使用-pthread,這根本沒有幫助。

我也嘗試過手動鏈接winpthreadpthread,但這也沒有做任何事情。

還有-std=c++11-std=gnu++11沒有什麼區別...

我真的失去了,現在不知道,如果它是在所有可能得到一個MinGW的版本,支持std::thread,但也許我只是忽略了一些編譯器標誌。我希望有人能幫助我!

+0

花費了幾個小時才找到解決方案,最後2點工作!謝謝! – Massimo

回答

10

你忘了加入您線:

t.join(); 
+2

好吧,我想我現在會自殺......但爲什麼它要回復這樣一個神祕的錯誤?它仍然不適用於所有版本,但至少有一個。謝謝! – Lukas

+3

銷燬可連接的線程會導致引發異常。由於你沒有捕捉到異常,程序終止。 –

3

僅供參考,已經有一個本機Win32實現的std ::線程和同步原語。它是一個僅包含頭文件的庫,適用於MinGW的任何C++ 11兼容版本。 https://github.com/meganz/mingw-std-threads

+0

這不是真正解決已經解決的問題的答案,因此評論會更合適。感謝分享! – Lukas

相關問題