Reputation: 1
For my assignment I need to write a multithreaded program that outputs all the prime numbers less than or equal to the number entered by the user, in a separate thread.
I am new to the threads and do not fully understand how to correctly implement them. But I created a boolean function which determines whether an integer is a prime number or not and I named it bool isPrime. However, the bool isPrime gives me the following error "declaration is incompatible with "LPVOID isPrime". I am not sure how to fix it, I've also noticed that my compiler (visual c++) gives a red underline for "cout" and "cin" but if I declare using namespace std, than the compiler underlines the "std" and gives this error "name must be a namespace name", I don't know what it supposed to mean. How could I fix these two problems?
#include <Windows.h>
DWORD Prime;
DWORD WINAPI primeThreadFun(LPVOID isPrime)
{
bool isPrime(int n)
{
float sqrtn = sqrt(n);
if (n < 2) return false;
if (n < 4) return true;
if (n % 2 == 0) return false;
for (int d = 3; d <= sqrtn; d += 2)
if (n % d == 0) return false;
return true;
}
}
int main(int argc, char *argv [])
{
DWORD ThreadId;
HANDLE ThreadHandle;
int n;
cout << "Enter a number: " << endl;
cin >> n;
for (int i = 0; i <= n; i++)
if (isPrime(i)) cout << i << " ";
cout endl;
}
ThreadHandle = CreateThread(NULL, 0, primeThreadFun, (LPVOID) isPrime, NULL, ThreadID []);
if (ThreadHandle != NULL)
{
WaitForSingleObject(ThreadHandle INFINITE);
CloseHandle(ThreadHandle);
}
}
Upvotes: 0
Views: 242
Reputation: 15872
First, CreateThread
requires a specific signature (reference):
DWORD WINAPI ThreadProc(LPVOID lpParam);
So your thread declaration will need the same signature.
Second, what you are attempting to do is not a good way to separate a thread. Think of a thread as a separate process. It will do some task and then end.
You need to do something like (NOTE: the following is not debugged and should only be used as a starting point - this is done intentionally as I'm not here to do your homework for you):
struct MyData
{
std::vector<unsigned int> myVec;
unsigned int myCap;
};
DWORD WINAPI MyThreadProc(LPVOID lpParam)
{
MyData* pData = (MyData*)lpParam;
for (int i = 4; i <= myCap; ++i)
{
int root = (int)std::sqrt(i);
bool isPrime = true;
for (int j = 2; j <= root; ++j)
{
if (i % j == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
pData->myVec.push_back(i);
}
}
}
int main()
{
MyData data;
std::cin >> data.myCap;
HANDLE hThread = CreateThread(NULL, 0, MyThreadProc, &data, NULL, NULL);
std:: cout << "Finding Primes" << std::endl;
while (WaitForSingleObject(hThread, 1000) != WAIT_OBJECT_0)
{
std::cout << ".";
}
std::cout << std::endl;
std::copy(data.begin(), data.end(), std::ostream_iterator<unsigned int>(std::cout, " "));
return 0;
}
Upvotes: 0
Reputation: 2096
C/C++ doesn't support nested functions -- isPrime is nested in primeThreadFun. Move it out, and call it from primeThreadFun.
also, you have a global variable named "isPrime" - you can't have that and a global function "isPrime()"
For cout you need to include "iostream"
Upvotes: 1