Firsly, allow me to post my code:
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <climits>
#include <thread>
#include <fstream>
using namespace std;
bool isPrime(int num) {
if (num < 2)
return false;
if (num == 2)
return true;
if (num % 2 == 0)
return false;
for (int i = 3; (i*i) <= num; i += 2) {
if (num % i == 0)
return false;
}
return true;
}
ofstream out;
void fwrite(unsigned int toWrite) { //function to write the value on file
out << toWrite << '\n';
}
void fprint(long double toPrint) { //function to print the percentage of the completion of the writing-on-file process
cout << setw(8) << '\r' << (long double)toPrint / 100000000.0 * 100 << "%";
}
int main(){
out.open("prime numbers from 5 to 100 millions");
for(unsigned int i = 0; i <= 100000000; i++){
if (isPrime(i)) {
thread tprint(fprint,i); //works fine alone
thread twrite(fwrite,i); //triggers intellisense and compile error
tprint.join();
twrite.join();
}
}
}
This program's purpose is to print and write to file all the prime numbers it can find, up to 100 millions. Printing and writing operations are done in two different threads. The functions resposible for this arefwrite
and fprint
.
If a prime number is found, thread tprint(fprint,i)
is caled, and the current percentage of completion is printed without any problem.
But if I use thread twrite(fwrite,i)
I get these compiler errors.
E0289 no instance of constructor "std::thread::thread" matches the argument list <the path to the cpp file...>\infprime.cpp line 37`
and
Severity Code Description Project File Line Suppression State C2661 'std::thread::thread': no overloaded function takes 2 arguments <the path to the cpp file..>\infprime.cpp line 37
I absolutely have no idea why thread tprint(fprint,i)
works but thread twrite(fwrite,i)
causes these errors.