AϟϟTERIX
AϟϟTERIX

Reputation: 101

Compile multithread application for Windows on Linux [ C++ ]

Can anybody provide an example of a command to compile an application that uses mutexes and threads with mingw. When I tried to do this with command i686-w64-mingw32-g++ -std=c++11 -lpthread -o myprog.exe myprog.cpp I got an Error that mutex is not declared main.cpp:15:1: error: ‘mutex’ does not name a type

Here is the code:

#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;
}

So... here is the code with using that library that was provided by @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;
} 

But I still have an error but this time it is error: declaration of ‘class std::future’

Upvotes: 1

Views: 438

Answers (1)

Smeeheey
Smeeheey

Reputation: 10316

You need to

  1. Ensure you #include <mutex> in your source file

  2. Either use std::mutex or have using namespace std at the top of your source file.

EDIT (having seen the code): It seems mingw32 still doesn't support C++11 threads natively. You can either switch to mingw64, or use this library, which adds std thread support.

Upvotes: 2

Related Questions