C ++线程错误:没有名为“ type”的MINGW类型

时间:2018-06-30 18:42:37

标签: c++ multithreading

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

所以我做到了:

#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版本。

1 个答案:

答案 0 :(得分:3)

声明

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

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

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

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