程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> 線程按指定次序輸入字符到數組的實例代碼

線程按指定次序輸入字符到數組的實例代碼

編輯:關於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;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved