Skip to content

Instantly share code, notes, and snippets.

@blueintegral
Created January 15, 2013 06:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blueintegral/4536625 to your computer and use it in GitHub Desktop.
Save blueintegral/4536625 to your computer and use it in GitHub Desktop.
Demonstrates effect of threading by calculating perfect numbers (an integer equal to the sum of its integer divisors with no remainders) and primes under 1,000,000 with and without threads.
// Perfect.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "Windows.h"
#include "LIMITS.h"
static HANDLE Thread_mutex;
int prime_count1;
int prime_count2;
int prime_count3;
int prime_count4;
static HANDLE Parameter1;
static HANDLE Parameter2;
static HANDLE Parameter3;
static HANDLE Parameter4;
int _tmain(int argc, _TCHAR* argv[])
{
prime_count1 = 0;
prime_count2 = 0;
prime_count3 = 0;
prime_count4 = 0;
HANDLE hThread1;
DWORD dwThread1ID = 0;
HANDLE hThread2;
DWORD dwThread2ID = 0;
HANDLE hThread3;
DWORD dwThread3ID = 0;
HANDLE hThread4;
DWORD dwThread4ID = 0;
int prime_count = 1; //counter
DWORD WINAPI PerfectThread1 (LPVOID Para);
DWORD WINAPI PerfectThread2 (LPVOID Para);
DWORD WINAPI PerfectThread3 (LPVOID Para);
DWORD WINAPI PerfectThread4 (LPVOID Para);
DWORD time_count;
unsigned long i, j, sum, prime;
printf("\n");
time_count = GetTickCount();
// count primes using only one thread
for (j=3; j<=100000; j=j+2)
21{
prime = 1;
for (i=2; i<j; i++)
{
if ((j % i) == 0){
prime = 0; //not prime if divisable by 2
break;
}
}
prime_count = prime_count + prime;
}
printf("%i prime numbers less than 1000000\n\r", prime_count);
time_count = GetTickCount() - time_count;
printf("\n\n %d milliseconds elapsed time\n\n\r", time_count);
Sleep(4000);
// now do it with four threads
// setup a semaphore for synchronization of threads with an initial value of 0 (waits on 0)
prime_count = 0;
time_count = GetTickCount();
Parameter1 = CreateMutex(NULL, FALSE, NULL);
hThread1 = CreateThread( NULL, 0, PerfectThread1, (LPVOID) Parameter1, 0, &dwThread1ID);
Parameter2 = CreateMutex(NULL, FALSE, NULL);
hThread2 = CreateThread( NULL, 0, PerfectThread2, (LPVOID) Parameter2, 0, &dwThread2ID);
Parameter3 = CreateMutex(NULL, FALSE, NULL);
hThread3 = CreateThread( NULL, 0, PerfectThread3, (LPVOID) Parameter3, 0, &dwThread3ID);
Parameter4 = CreateMutex(NULL, FALSE, NULL);
hThread4 = CreateThread( NULL, 0, PerfectThread4, (LPVOID) Parameter4, 0, &dwThread4ID);
// waits for all four threads to signal done with release semaphore
WaitForSingleObject(Parameter1 , INFINITE);
WaitForSingleObject(Parameter2, INFINITE);
WaitForSingleObject(Parameter3, INFINITE);
WaitForSingleObject(Parameter4, INFINITE);
printf("%i prime numbers less than 1000000\n\r", prime_count1 + prime_count2 + prime_count3 + prime_count4);
time_count = GetTickCount() - time_count;
printf("\n\n %d milliseconds elapsed time\n\r", time_count);
Sleep(4000);
CloseHandle(hThread1);
CloseHandle(hThread2);
CloseHandle(hThread3);
CloseHandle(hThread4);
return 0;
}
// code for each of the threads to execute
DWORD WINAPI PerfectThread1 (LPVOID Para1) {
unsigned long i, j, prime;
for (j=1 ; j<=40000; j = j+2){
prime = 1;
for (i=2; i<j; i++)
{
if ((j % i) == 0){
prime = 0; //not prime
break;
}
}
prime_count1 = prime_count1 + prime;
}
printf("%i \n", prime_count1);
ReleaseMutex(Para1);
return 0;
}
DWORD WINAPI PerfectThread2 (LPVOID Para2) {
unsigned long i, j, prime;
for (j=40001 ; j<=60000; j = j+2){
prime = 1;
for (i=2; i<j; i++)
{
if ((j % i) == 0){
prime = 0; //not prime if divisable by 2
break;
}
}
prime_count2 = prime_count2 + prime;
}
printf("%i \n", prime_count2);
ReleaseMutex(Para2);
return 0;
}
DWORD WINAPI PerfectThread3 (LPVOID Para3) {
unsigned long i, j, prime;
for (j=60001 ; j<=80000; j = j+2){
prime = 1;
for (i=2; i<j; i++)
{
if ((j % i) == 0){
prime = 0; //not prime if divisable by 2
break;
}
}
prime_count3 = prime_count3 + prime;
}
printf("%i \n", prime_count3);
ReleaseMutex(Para3);
return 0;
}
DWORD WINAPI PerfectThread4 (LPVOID Para4) {
unsigned long i, j, prime;
for (j=80001; j<=100000; j = j+2){
prime = 1;
for (i=2; i<j; i++)
{
if ((j % i) == 0){
prime = 0; //not prime if divisable by 2
break;
}
}
prime_count4 = prime_count4 + prime;
}
printf("%i \n", prime_count4);
ReleaseMutex(Para4);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment