The thread outputs the characters to the array in the specified order
- 2020-04-02 02:06:54
- OfStack
Topic:
There are three threads. The function of thread 1 is to output A to the character array, the function of thread 2 is to output B to the character array, and the function of thread 2 is to output C to the character array, and the function of thread 2 is to assign ABCABCABC to the array in order. The number of ABC is specified by the parameter of thread function 1.
Interface description:
Void Init (); // initializes the function
Void Release (); // resource release function
Unsignedint__stdcall ThreadFun1 PVOID (pM) ; // thread function 1, passing in an int pointer, used to initialize the output A times, resources need to be released by the thread
Unsignedint__stdcall ThreadFun2 PVOID (pM) ; // thread function 2, no parameters passed in
Unsignedint__stdcall ThreadFun3 PVOID (pM) ; // thread function 3, no parameter passed in
char G_write [1024]. // thread 1, thread 2, and thread 3 assign values to the array in descending order. Do not worry about whether the array overbounds, test case guarantee
The source code:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <process.h>
#include <windows.h>
#define MAXHANDLE 3
char g_write[1028]; //Thread 1, thread 2, and thread 3 assign values to the array in descending order
HANDLE g_hThreadEvent[3];
HANDLE handle[MAXHANDLE];
int g_Number;
//Thread 1 function & NBSP; & have spent
unsigned int __stdcall ThreadFun1(PVOID pM)
{
int uiNumber = *(int *)pM;
int iLoop = 0;
g_Number = uiNumber;
for (iLoop; iLoop < uiNumber; iLoop++)
{
//printf("this is thread 1: %sn", g_write);
WaitForSingleObject(g_hThreadEvent[0], INFINITE);
strcat(g_write, "A");
SetEvent(g_hThreadEvent[1]);
}
_endthreadex(0);
return 0;
}
//Thread 2 function & NBSP; & have spent
unsigned int __stdcall ThreadFun2(PVOID pM)
{
int iLoop = 0;
for (iLoop; iLoop < g_Number; iLoop++)
{
//printf("this is thread 2: %sn", g_write);
WaitForSingleObject(g_hThreadEvent[1], INFINITE);
strcat(g_write, "B");
SetEvent(g_hThreadEvent[2]);
}
_endthreadex(0);
return 0;
}
//Thread 3 function & NBSP; & have spent
unsigned int __stdcall ThreadFun3(PVOID pM)
{
int iLoop = 0;
for (iLoop; iLoop < g_Number; iLoop++)
{
//printf("this is thread 2: %sn", g_write);
WaitForSingleObject(g_hThreadEvent[2], INFINITE);
strcat(g_write, "C");
SetEvent(g_hThreadEvent[0]);
}
_endthreadex(0);
return 0;
}
void Init(void)
{
g_hThreadEvent[0] = CreateEvent(NULL, FALSE, TRUE, NULL);
g_hThreadEvent[1] = CreateEvent(NULL, FALSE, FALSE, NULL);
g_hThreadEvent[2] = CreateEvent(NULL, FALSE, FALSE, NULL);
memset(g_write, NULL, sizeof(g_write));
}
void Release(void)
{
int iLoop = 0;
for (int iLoop = 0; iLoop < MAXHANDLE; iLoop++)
{
CloseHandle(handle[iLoop]);
}
}
int main( int Argc, char* Argv[])
{
int uiNumber = 10; //The number of times it needs to be reprinted
int *num = NULL;
Init();
num = (int*)malloc(sizeof(int));
*num = uiNumber;
handle[0] = (HANDLE)_beginthreadex(NULL, 0, ThreadFun1, num, 0, NULL);
handle[1] = (HANDLE)_beginthreadex(NULL, 0, ThreadFun2, NULL, 0, NULL);
handle[2] = (HANDLE)_beginthreadex(NULL, 0, ThreadFun3, NULL, 0, NULL);
WaitForMultipleObjects(MAXHANDLE, handle, TRUE, INFINITE);
Release();
printf("g_write = %sn", g_write);
system("pause");
return 0;
}