gpt4 book ai didi

C++ 线程错误 : no type named ‘type’ MINGW

转载 作者:行者123 更新时间:2023-11-27 23:45:23 25 4
gpt4 key购买 nike

我目前正在为我的操作系统类做一个项目,我必须编写一个程序来查找要在线程中运行的素数。

所以我这样做了:

#include <iostream>
#include <cmath>
#include <thread>
#define THREADNUMBER 100
using namespace std;


int CONTADOR = 0;
int CONTADORTHREADEXECUTADA = 0;
int primeRange(int v1, int v2) {
int a, limit;
bool isprime;

for (int i = v1; i <= v2; i++) {
if (i % 2 != 0 && i % 3 != 0) {
isprime = true;
limit = i / 2;
if(i == 1) isprime =false;


limit = (int)sqrt(i); //General case

for(a=2; a <= limit; a++){
if(i % i == 0 && i != 2){
isprime = false;
break;
}
}
if (isprime) {
CONTADOR++;
}

}
}
CONTADORTHREADEXECUTADA++;
return 1;
}
int main(int argc, char *argv[ ] ) {
int number1 = atoi(argv[1]);
int number2 = atoi(argv[2]);
int dif = number2-number1;
thread** vec = new thread*[THREADNUMBER];
cout<< "criando threads" <<endl;
int contadorthread = 0;
if (dif <= THREADNUMBER) {
for(int i = number1; i <= number2; i++) {
thread* t = new thread(primeRange(i,i));
vec[contadorthread] = t;
contadorthread++;
}
} else {
int c = dif / THREADNUMBER;

for(int i = number1; i <= number2; i+=(c+1)) {
if (contadorthread==THREADNUMBER-1) {
thread* t = new thread(primeRange(i,number2));
vec[contadorthread] = t;
contadorthread++;
break;
}
thread* t = new thread(primeRange(i,i+c));
vec[contadorthread] = t;
contadorthread++;

}
cout<<contadorthread << " threads criadas"<<endl;
cout<<"inicializando threads" <<endl;

while (CONTADORTHREADEXECUTADA < contadorthread) {
cout<<contadorthread - CONTADORTHREADEXECUTADA << endl;
}
cout<< CONTADOR << "primos encontrados" <<endl;
}
}

但每次我尝试在 64 位 Windows GCC 上编译时,我都会收到此错误消息,指出没有名为“type”的类型

我用这个编译

g++ -Wall -g -std=c++11 -pthread codigo.cpp -o exe -Wall

我能做什么?我已经下载了他们所说的线程安全的 mingw 版本。

最佳答案

声明

thread* t = new thread(primeRange(i,i));

调用函数 primeRange 并将 result(int) 传递给线程构造函数,这显然不是调用方式。而是使用:

thread* t = new thread(primeRange, i, i);

它将函数指针和参数传递给线程构造函数。

关于C++ 线程错误 : no type named ‘type’ MINGW,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51117605/

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