欢迎来到代码驿站!

C代码

当前位置:首页 > 软件编程 > C代码

线程按指定顺序输出字符到数组的实例代码

时间:2021-03-08 11:39:27|栏目:C代码|点击:

题目:

有三个线程,线程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;
}

上一篇:Visual Studio Code 2020安装教程及CPP环境配置(教程图解)

栏    目:C代码

下一篇:对C++默认构造函数的一点重要说明

本文标题:线程按指定顺序输出字符到数组的实例代码

本文地址:http://www.codeinn.net/misctech/76702.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有