标签:
/* 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 not 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 && slot < 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);
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
TaskQueS.c---分配工作并以WaitForSingleObject()等待之
标签:
原文地址:http://blog.csdn.net/wangfengfan1/article/details/47019349