線程按指定順序輸出字符到數(shù)組的實例代碼
題目:
有三個線程,線程1的功能就是向字符數(shù)組輸出A,線程2的功能就是向字符數(shù)組輸出B,線程2的功能就是向字符數(shù)組輸出C,要求按順序向數(shù)組賦值A(chǔ)BCABCABC,ABC的個數(shù)由線程函數(shù)1的參數(shù)指定。
接口說明:
void Init(); //初始化函數(shù)
void Release(); //資源釋放函數(shù)
unsignedint__stdcall ThreadFun1(PVOID pM) ; //線程函數(shù)1,傳入一個int類型的指針,用于初始化輸出A次數(shù),資源需要線程釋放
unsignedint__stdcall ThreadFun2(PVOID pM) ;//線程函數(shù)2,無參數(shù)傳入
unsignedint__stdcall ThreadFun3(PVOID pM) ;//線程函數(shù)3,無參數(shù)傳入
char g_write[1024]; //線程1,線程2,線程3按到順序向該數(shù)組賦值。不用考慮數(shù)組是否越界,測試用例保證
源代碼:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <process.h>
#include <windows.h>
#define MAXHANDLE 3
char g_write[1028]; //線程1,線程2,線程3按到順序向該數(shù)組賦值
HANDLE g_hThreadEvent[3];
HANDLE handle[MAXHANDLE];
int g_Number;
//線程1函數(shù)
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: %s\n", g_write);
WaitForSingleObject(g_hThreadEvent[0], INFINITE);
strcat(g_write, "A");
SetEvent(g_hThreadEvent[1]);
}
_endthreadex(0);
return 0;
}
//線程2函數(shù)
unsigned int __stdcall ThreadFun2(PVOID pM)
{
int iLoop = 0;
for (iLoop; iLoop < g_Number; iLoop++)
{
//printf("this is thread 2: %s\n", g_write);
WaitForSingleObject(g_hThreadEvent[1], INFINITE);
strcat(g_write, "B");
SetEvent(g_hThreadEvent[2]);
}
_endthreadex(0);
return 0;
}
//線程3函數(shù)
unsigned int __stdcall ThreadFun3(PVOID pM)
{
int iLoop = 0;
for (iLoop; iLoop < g_Number; iLoop++)
{
//printf("this is thread 2: %s\n", 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; //需要重復(fù)打印的次數(shù)
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 = %s\n", g_write);
system("pause");
return 0;
}
相關(guān)文章
C++ STL 內(nèi) std::{bind/tuple/function} 簡單實現(xiàn)
這篇文章主要介紹了C++ STL 內(nèi) std::{bind/tuple/function} 簡單實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02LeetCode 單調(diào)棧內(nèi)容小結(jié)
這篇文章主要介紹了LeetCode 單調(diào)棧內(nèi)容小結(jié),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C++實現(xiàn)LeetCode(35.搜索插入位置)
這篇文章主要介紹了C++實現(xiàn)LeetCode(35.搜索插入位置),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07