TASKQUES.C---Assign work and wait with WaitForSingleObject ()

Source: Internet
Author: User

/* 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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.