/* Call ThreadFunc num_tasks times,using No more than thread_pool_size threads. Thiss
Version uses Waitforsingleobject,which gives a very suboptimal solution.*/
Busywait.c
/*domonstrate the effect on performance of using a busy loop.
First call the worker routine with just a function call to get a baseline performance reading
Then create a second thread and a busy loop.
*/
#define Win32_lean_and_mean
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#include "MtVerify.h"
DWORD WINAPI ThreadFunc (LPVOID);
#define THREAD_POOL_SIZE 3
#define MAX_THREAD_INDEX Thread_pool_size-1
#define NUM_TASKS 6
int main ()
{
HANDLE Hthrds[thread_pool_size];
int slot = 0;
DWORD threadId;
int i;
DWORD ExitCode;
/* i= 1 2 3 4 5 6 7 8 9
* Start Thread x x x x x x
* Wait on thread X x x x x x
*/
for (i = 1; I <= num_tasks; i++)
{
if (i > Thread_pool_size)
{
WaitForSingleObject (Hthrds[slot], INFINITE);
Mtverify (GetExitCodeThread (Hthrds[slot], &exitcode));
printf ("Slot%d terminated \ n", ExitCode);
Mtverify (CloseHandle (Hthrds[slot));
}
Mtverify (Hthrds[slot] = CreateThread (NULL, 0, ThreadFunc, (LPVOID) slot, 0, &threadid));
printf ("Launched thread #%d (slot%d) \ n", I, slot);
if (++slot > Max_thread_index)
slot = 0;
for (slot = 0; slot < thread_pool_size; slot++)
{
WaitForSingleObject (Hthrds[slot], INFINITE);
Mtverify (CloseHandle (Hthrds[slot));
}
printf ("All Slots terminated\n");
System ("pause");
return exit_success;
}
}
/*this function just calls Sleep for
A random amount of time, thereby simulating
Some tasks that takes time.
The Param "n" is the index into the handle array,
Kept for informational purposes.
*/
DWORD WINAPI ThreadFunc (lpvoid N)
{
Srand (GetTickCount ());
Sleep (rand ()% 8) * 500 + 500);
printf ("Slot%d idle\n", n);
Return ((DWORD) n);
}
WaitForMultipleObjects ()
Busywait.c
/*domonstrate the effect on performance of using a busy loop.
First call the worker routine with just a function call to get a baseline performance reading
Then create a second thread and a busy loop.
*/
#define Win32_lean_and_mean
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#include "MtVerify.h"
DWORD WINAPI ThreadFunc (LPVOID);
#define THREAD_POOL_SIZE 3
#define MAX_THREAD_INDEX Thread_pool_size-1
#define NUM_TASKS 6
int main ()
{
HANDLE Hthrds[thread_pool_size];
int slot = 0;
DWORD threadId;
int i;
DWORD RC;
/* i= 1 2 3 4 5 6 7 8 9
* Start Thread x x x x x x
* Wait on thread X x x x x x
*/
for (i = 1; I <= num_tasks; i++)
{/*until we ' ve used all threads in the pool,do don't need to wait for one to exit*/
if (i > Thread_pool_size)
{
Wait for one thread to terminate
Rc=waitformultipleobjects (Thread_pool_size, hthrds,false,infinite);
slot = Rc-wait_object_0;
Mtverify (slot >= 0 && slots < thread_pool_size);
printf ("Slot%d terminated\n", slot);
Mtverify (CloseHandle (Hthrds[slot));
}
/* Create A new thread in the given available slot*/
Mtverify (hthrds[slot++] = CreateThread (NULL, 0, ThreadFunc,
(LPVOID) slot, 0, &threadid));
printf ("Launched thread #%d (slot%d) \ n", I, slot);
/*now wait for all threads to terminate*/
rc = WaitForMultipleObjects (Thread_pool_size, Hthrds, TRUE, INFINITE);
Mtverify (RC >= wait_object_0 && RC < WAIT_OBJECT_0 + thread_pool_size);
for (slot = 0; slot < thread_pool_size; slot++)
{
Mtverify (CloseHandle (Hthrds[slot));
}
printf ("All Slots terminated\n");
System ("pause");
return exit_success;
}
}
/*this function just calls Sleep for
A random amount of time, thereby simulating
Some tasks that takes time.
The Param "n" is the index into the handle array,
Kept for informational purposes.
*/
DWORD WINAPI ThreadFunc (lpvoid N)
{
Srand (GetTickCount ());
Sleep (rand ()% 8) * 500 + 500);
printf ("Slot%d idle\n", n);
Return ((DWORD) n);
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
TASKQUES.C---Assign work and wait with WaitForSingleObject ()