gpt4 book ai didi

c++ - 在 Linux [C++] 上为 Windows 编译多线程应用程序

转载 作者:行者123 更新时间:2023-11-30 03:41:04 26 4
gpt4 key购买 nike

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

代码如下:

#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’的声明

最佳答案

你需要

  1. 保证你#include <mutex>在你的源文件中

  2. 要么使用 std::mutex或者有 using namespace std在源文件的顶部。

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

关于c++ - 在 Linux [C++] 上为 Windows 编译多线程应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37565579/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com