Hi Guys,
I got an assignment to calculate the execute time difference a code written with threads and one without. I got the algorithm working but GetTickCount() return the same number before and after the code runs. Any help is much appreciated!
When I run my code, I get
13611602
13611602
# of prime < 10000 is 1229
0 ms elapsed time
# of prime < 10000 is 1229
0 ms elapsed time
Why doesn't GetTickCount() update itself after running the for loop to count the number of prime #s < 1000? Thanks.
I've copied the code below:
// Prime.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "Windows.h"
#include "LIMITS.h"
#include <iostream>
using namespace std;
static HANDLE Thread_semaphore;
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hThread1;
DWORD dwThread1ID = 0;
HANDLE hThread2;
DWORD dwThread2ID = 0;
HANDLE hThread3;
DWORD dwThread3ID = 0;
HANDLE hThread4;
DWORD dwThread4ID = 0;
INT nParameter = 1;
DWORD WINAPI PerfectThread (LPVOID lpArg);
DWORD WINAPI PrimeThread (LPVOID lpArg);
DWORD time_count;
unsigned long i, j, sum;
printf("\n");
int pcount = 0;
int pstart = 2; // Start from 2
int pend = 10000;
time_count = GetTickCount();
cout << time_count << endl;
for(;pstart <= pend; pstart++)
{
int trialDivisor = 2;
int prime = 1;
for(; trialDivisor * trialDivisor <= pstart; trialDivisor++)
{
if(pstart % trialDivisor == 0)
{
prime = 0;
break;
}
}
if(prime) pcount++;
}
cout << GetTickCount() << endl;
time_count = GetTickCount() - time_count;
printf("# of prime < %d is %d", pend, pcount);
printf("\n\n %d milliseconds elapsed time\n\n\r", time_count);
Sleep(1000);
// do it with threads
Thread_semaphore = CreateSemaphore(NULL, 0, 4, TEXT("Thread_Done"));
time_count = GetTickCount();
hThread1 = CreateThread( NULL, 0, PrimeThread, (LPVOID)nParameter, 0, &dwThread1ID);
nParameter++;
hThread2 = CreateThread( NULL, 0, PrimeThread, (LPVOID)nParameter, 0, &dwThread2ID);
nParameter++;
hThread3 = CreateThread( NULL, 0, PrimeThread, (LPVOID)nParameter, 0, &dwThread3ID);
nParameter++;
hThread4 = CreateThread( NULL, 0, PrimeThread, (LPVOID)nParameter, 0, &dwThread4ID);
WaitForSingleObject(Thread_semaphore, INFINITE);
WaitForSingleObject(Thread_semaphore, INFINITE);
WaitForSingleObject(Thread_semaphore, INFINITE);
WaitForSingleObject(Thread_semaphore, INFINITE);
time_count = GetTickCount() - time_count;
printf("\n\n %d milliseconds elapsed time\n\r", time_count);
Sleep(1000);
CloseHandle(hThread1);
CloseHandle(hThread2);
CloseHandle(hThread3);
CloseHandle(hThread4);
getchar();
return 0;
}
DWORD WINAPI PrimeThread (LPVOID lpArg)
{
int pend = 10000;
int pstart = 2;
int pcount = 0;
INT threadnumber = (INT) lpArg;
unsigned long i, j, sum;
for (j=1 + threadnumber; j<=10000; j=j+4)
{
for(;pstart <= pend; pstart++)
{
int trialDivisor = 2;
int prime = 1;
for(; trialDivisor * trialDivisor <= pstart; trialDivisor++)
{
if(pstart % trialDivisor == 0)
{
prime = 0;
break;
}
}
if(prime) pcount++;
}
}
printf("# of prime < %d is %d", pend, pcount);
ReleaseSemaphore(Thread_semaphore, 1, NULL);
return 0;
}