Question: Receiving the following error in my code: DWORD ( _ _ stdcall * ) ( LPVOID threadNo ) is incompatible with parameter of type

Receiving the following error in my code: DWORD (__stdcall *)(LPVOID threadNo)" is incompatible with parameter of type "_beginthreadex_proc_type"
#include
#include
#include
#include
bool enableMutex = false; //Command line parameter
int count =0; //Actual count after update by threads
unsigned long numberIter =0; //Command line parameter
HANDLE mutex; // Mutex for critical section
DWORD WINAPI threadWork(LPVOID threadNo)
{
int threadID =*(int*)threadNo;
for (unsigned int i =0; i < numberIter; i++)
{
if (enableMutex)
{
WaitForSingleObject(mutex, INFINITE);
}
for (int j =0; j <10; j++)
{
count += abs(static_cast(GetCurrentThreadId()));
Sleep(1);
}
if (enableMutex)
{
ReleaseMutex(mutex);
}
}
std::cout << "Thread "<< threadID <<" terminated." << std::endl;
return 0;
}
int main(int argc, char* argv[])
{
// Get the command line parameters
if (argc !=4)
{
std::cerr << "Invalid number of arguments." << std::endl;
return 1;
}
int numberOfThreads = atoi(argv[1]);
numberIter = atol(argv[2]);
enableMutex = atoi(argv[3])!=0;
// Initialize mutex
if (enableMutex)
{
mutex = CreateMutex(NULL, FALSE, NULL);
if (mutex == NULL)
{
std::cerr << "Failed to create mutex." << std::endl;
return 1;
}
}
// Create threads
HANDLE* threads = new HANDLE[numberOfThreads];
int* threadIDs = new int[numberOfThreads];
for (int i =0; i < numberOfThreads; i++)
{
threadIDs[i]= i;
threads[i]=(HANDLE)_beginthreadex(NULL,0, &threadWork, &threadIDs[i],0, NULL);
if (threads[i]== NULL)
{
std::cerr << "Failed to create thread "<< i <<"."<< std::endl;
return 1;
}
std::cout << "Thread "<< i <<" created." << std::endl;
}
// Wait for threads to terminate
WaitForMultipleObjects(numberOfThreads, threads, TRUE, INFINITE);
// Output the actual value of count and the theoretical value
std::cout << "Actual count ="<< count << std::endl;
int expectedCount = numberOfThreads * numberIter *10*((numberOfThreads -1)* numberOfThreads /2+ numberOfThreads);
std::cout << "Expected count ="<< expectedCount << std::endl;
// Clean up
if (enableMutex)
{
CloseHandle(mutex);
}
delete[] threads;
delete[] threadIDs;
return 0
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!