时间:2021-05-19
题目:
有三个线程,线程1的功能就是向字符数组输出A,线程2的功能就是向字符数组输出B,线程2的功能就是向字符数组输出C,要求按顺序向数组赋值ABCABCABC,ABC的个数由线程函数1的参数指定。
接口说明:
void Init(); //初始化函数
void Release(); //资源释放函数
unsignedint__stdcall ThreadFun1(PVOID pM) ; //线程函数1,传入一个int类型的指针,用于初始化输出A次数,资源需要线程释放
unsignedint__stdcall ThreadFun2(PVOID pM) ;//线程函数2,无参数传入
unsignedint__stdcall ThreadFun3(PVOID pM) ;//线程函数3,无参数传入
char g_write[1024]; //线程1,线程2,线程3按到顺序向该数组赋值。不用考虑数组是否越界,测试用例保证
源代码:
复制代码 代码如下:
#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按到顺序向该数组赋值
HANDLE g_hThreadEvent[3];
HANDLE handle[MAXHANDLE];
int g_Number;
//线程1函数
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函数
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函数
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; //需要重复打印的次数
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;
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
C++字符数组的输入输出字符数组的输入输出可以有两种方法:1)逐个字符输入输出。2)将整个字符串一次输入或输出。例如有以下程序段:charstr[20];cin
下面给大家分享一小段代码给大家介绍C#输出字符串到文本文件中,具体代码如下所示:publicclassWriteHelper{publicstaticvoidW
从字符数组中删除特定的字符,即输入要删除的指定字符,利用for循环找到非指定字符,将非指定字符输出即可。以下为具体的实现方法:#includeintmain()
java实现输出字符串中第一个出现不重复的字符详解比如:输入name输出n,输入teeter输出r,输入namename输出null具体实现代码如下:impor
实例如下:$var=explode("|",$str);把$str按|进行分割php还有其他的把字符串指定字符分割成数组str_split(string,len