【问题标题】:Compile multithread application for Windows on Linux [ C++ ]在 Linux [ C++ ] 上为 Windows 编译多线程应用程序
【发布时间】:2016-06-01 10:06:01
【问题描述】:

任何人都可以提供一个命令示例来编译一个使用互斥锁和线程的应用程序。 当我尝试使用命令i686-w64-mingw32-g++ -std=c++11 -lpthread -o myprog.exe myprog.cpp 执行此操作时,我收到一个错误,即未声明互斥锁main.cpp:15:1: error: ‘mutex’ does not name a type

代码如下:

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

但我仍然有一个错误,但这次是错误:'class std::future'的声明

【问题讨论】:

    标签: c++ multithreading mingw mingw32 mingw-w64


    【解决方案1】:

    你需要

    1. 确保您的源文件中有#include &lt;mutex&gt;

    2. 在源文件的顶部使用std::mutexusing namespace std

    编辑(看过代码):似乎 mingw32 本身仍然不支持 C++11 线程。您可以切换到 mingw64,也可以使用 this library,它添加了 std 线程支持。

    【讨论】:

    • 我仍然有一个错误,但这次是错误:'class std::future'的声明
    • 对不起,我不是那个错误)这个错误:'class std::future'的声明
    • 看起来你运气不好:图书馆不提供std::future。你改变你的设计不使用它们,或者使用不同的编译器(顺便说一句,我可以指出在 Linux 上交叉编译 Windows 程序非常困难,在完成之前你可能会面临更多问题)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 2017-05-22
    相关资源
    最近更新 更多