2016-06-01 4 views
1

Может ли кто-нибудь предоставить пример команды для компиляции приложения, которое использует мьютексы и потоки с mingw. Когда я попытался сделать это с помощью команды i686-w64-mingw32-g++ -std=c++11 -lpthread -o myprog.exe myprog.cpp я получил ошибку, что мьютекс не объявлен main.cpp:15:1: error: ‘mutex’ does not name a typeКомпилировать многопоточное приложение для Windows на Linux [C++]

Вот код:

#include <iostream> 
#include <thread> 
#include <vector> 
#include <future> 
#include <mutex> 
#include <ctime> 
#include <cstring> 

using namespace std; 


#define MAX_SIZE_OF_THE_WORD 15 
int nCount = 0; 

mutex Mutex; 



char * MakeWord(){ 
    srand (time(NULL)); 
    int Size = rand() % MAX_SIZE_OF_THE_WORD; 
    char Array[Size]; 
    for (auto &w : Array){ 
     srand (time(NULL)); 
     w = (char)(rand()%50); 
    } 
    return Array; 
} 



bool HelloWorldShow(char * YOUR_TEXT){ 
    lock_guard<mutex> M(Mutex); 
    cout<<"Hello World number --- "<< nCount <<" And here is your text --- "<<YOUR_TEXT<<endl; 
    nCount++; 
    if (strlen(YOUR_TEXT) > 3) { 
     return true; 
    } 
    else{ 
     throw runtime_error(false); 
    } 

} 



int main() { 

    int nNum; 
    cout<<"Enter number of threads"<<endl; 
    cin>>nNum; 
    if (nNum >= 50){ 
     cout<< "Restart program and open less than 50 threads"<<endl; 
     return 0; 
    } 

    vector<future<bool>> a_Futures; 
    const char * Words[nNum]; 
    for (auto &w : Words){ 
     w = MakeWord(); 
    } 

    for(int i =0; i< nNum; i++){ 
     a_Futures.push_back(async(launch::async, &HelloWorldShow, (char *)Words[i])); 
    } 




     try { 
      for(auto &f : a_Futures) { 
       bool nRes = f.get(); 
      } 

     } 
     catch (exception & ex) { 
      cout<<"Exiting..."<<endl; 
     } 




    return 0; 
} 

Итак ... вот код с помощью этой библиотеки, которая была обеспечивается @Smeeheey

#undef _GLIBCXX_HAS_GTHREADS 
#include <iostream> 
#include "mingw.thread.h" 
#include <mutex> 
#include "mingw.mutex.h" 
#include "mingw.condition_variable.h" 
#include <vector> 
#include <future> 
#include <ctime> 
#include <cstring> 

using namespace std; 


#define MAX_SIZE_OF_THE_WORD 15 
int nCount = 0; 

mutex Mutex; 



char * MakeWord(){ 
    srand (time(NULL)); 
    int Size = rand() % MAX_SIZE_OF_THE_WORD; 
    char Array[Size]; 
    for (auto &w : Array){ 
     srand (time(NULL)); 
     w = (char)(rand()%50); 
    } 
    return Array; 
} 



bool HelloWorldShow(char * YOUR_TEXT){ 
    lock_guard<mutex> M(Mutex); 
    cout<<"Hello World number --- "<< nCount <<" And here is your text --- "<<YOUR_TEXT<<endl; 
    nCount++; 
    if (strlen(YOUR_TEXT) > 3) { 
     return true; 
    } 
    else{ 
     throw runtime_error(false); 
    } 

} 



int main() { 

    int nNum; 
    cout<<"Enter number of threads"<<endl; 
    cin>>nNum; 
    if (nNum >= 50){ 
     cout<< "Restart program and open less than 50 threads"<<endl; 
     return 0; 
    } 

    vector<future<bool>> a_Futures; 
    const char * Words[nNum]; 
    for (auto &w : Words){ 
     w = MakeWord(); 
    } 

    for(int i =0; i< nNum; i++){ 
     a_Futures.push_back(async(launch::async, &HelloWorldShow, (char *)Words[i])); 
    } 




     try { 
      for(auto &f : a_Futures) { 
       bool nRes = f.get(); 
      } 

     } 
     catch (exception & ex) { 
      cout<<"Exiting..."<<endl; 
     } 




    return 0; 
} 

Но я до сих пор есть ошибка, но на этот раз это ошибка: декларирование «класса станд :: будущего»

ответ

2

Вы должны

  1. Убедитесь, что вы #include <mutex> в исходном файле

  2. std::mutex Либо использовать или иметь using namespace std в верхней части исходного файла.

EDIT (видевший код): Кажется, mingw32 еще не поддерживает C++ 11 темы изначально. Вы можете либо переключиться на mingw64, либо использовать this library, который добавляет поддержку потока std.

+0

Я добавил код –

+0

Можете ли вы добавить точное сообщение об ошибке – Smeeheey

+0

Добавлено сообщение об ошибке –