面试题-多线程编程

题目:

四个线程 t1,t2,t3,t4,向 4 个文件中写入数据, t1 只能写入 1, t2 只能写入 2, t3 只能写
入 3, t4 只能写入 4,对 4 个文件 A, B, C, D 写入如下内容
A:123412341234.....
B:234123412341....
C:341234123412....
D:412341234123....

 

怎么实现同步可以让线程并行工作?

用windows c++实现.

// 222_thread.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include 
#include 
#include 
using namespace std;



HANDLE ghMutex;
HANDLE handles[4][4];
int idxx = 1;

void threadA()
{
    int idx = 0;
    do
    {
        DWORD res = WaitForMultipleObjects(4, handles[0], false, INFINITE);

        WaitForSingleObject(ghMutex, INFINITE);
        switch (res)
        {
        case WAIT_OBJECT_0:
            cout << "A"  ;
            SetEvent(handles[1][0]);
            break;
        case WAIT_OBJECT_0+1:
            cout << "B";
            SetEvent(handles[1][1]);
            break;
        case WAIT_OBJECT_0+2:
            cout << "C";
            SetEvent(handles[1][2]);
            break;
        case WAIT_OBJECT_0+3:
            cout << "D";
            SetEvent(handles[1][3]);
            break;
        }
        cout << 1;
        idxx++;
        ReleaseMutex(ghMutex);
    } while (idxx < 50);
    
}

void threadB()
{    
    int idx = 0;
    do
    {
        DWORD res = WaitForMultipleObjects(4, handles[1], false, INFINITE);
        WaitForSingleObject(ghMutex, INFINITE);
        switch (res)
        {
        case WAIT_OBJECT_0:
            cout << "A";
            SetEvent(handles[2][0]);
            break;
        case WAIT_OBJECT_0 + 1:
            cout << "B";
            SetEvent(handles[2][1]);
            break;
        case WAIT_OBJECT_0 + 2:
            cout << "C";
            SetEvent(handles[2][2]);
            break;
        case WAIT_OBJECT_0 + 3:
            cout << "D";
            SetEvent(handles[2][3]);
            break;
        }
        cout << 2;
        idxx++;
        ReleaseMutex(ghMutex);
    } while (idxx < 50);
}
void threadC()
{
    int idx = 0;
    do
    {
        DWORD res = WaitForMultipleObjects(4, handles[2], false, INFINITE);
        WaitForSingleObject(ghMutex, INFINITE);
        switch (res)
        {
        case WAIT_OBJECT_0:
            cout << "A";
            SetEvent(handles[3][0]);
            break;
        case WAIT_OBJECT_0 + 1:
            cout << "B";
            SetEvent(handles[3][1]);
            break;
        case WAIT_OBJECT_0 + 2:
            cout << "C";
            SetEvent(handles[3][2]);
            break;
        case WAIT_OBJECT_0 + 3:
            cout << "D";
            SetEvent(handles[3][3]);
            break;
        }
        cout << 3;
        idxx++;
        ReleaseMutex(ghMutex);
    } while (idxx < 50);
}
void threadD()
{
    int idx = 0;
    do
    {
        DWORD res = WaitForMultipleObjects(4, handles[3], false, INFINITE);
        WaitForSingleObject(ghMutex, INFINITE);
        switch (res)
        {
        case WAIT_OBJECT_0:
            cout << "A";
            SetEvent(handles[0][0]);
            break;
        case WAIT_OBJECT_0 + 1:
            cout << "B";
            SetEvent(handles[0][1]);
            break;
        case WAIT_OBJECT_0 + 2:
            cout << "C";
            SetEvent(handles[0][2]);
            break;
        case WAIT_OBJECT_0 + 3:
            cout << "D";
            SetEvent(handles[0][3]);
            break;
        }
        cout << 4;
        idxx++;
        ReleaseMutex(ghMutex);
    } while (idxx < 50);
}

void WriteA()
{
    SetEvent(handles[0][0]);
}
void WriteB()
{
    SetEvent(handles[1][1]);
}
void WriteC()
{
    SetEvent(handles[2][2]);
}
void WriteD()
{
    SetEvent(handles[3][3]);
}

int _tmain(int argc, _TCHAR* argv[])
{
    /*            file A      file B        file C       file D
      thread 1 handle[0][0] handle[0][1] handle[0][2] handle[0][3] 
      thread 2 handle[1][0] handle[1][1] handle[1][2] handle[1][3]
      thread 3 handle[2][0] handle[2][1] handle[2][2] handle[2][3]
      thread 4 handle[3][0] handle[3][1] handle[3][2] handle[3][3]
    */

    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            handles[i][j] = CreateEvent(NULL, false, false, NULL);
        }
    }

    DWORD threadID;
    CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)threadA, NULL, 0, &threadID);
    CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)threadB, NULL, 0, &threadID);
    CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)threadC, NULL, 0, &threadID);
    CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)threadD, NULL, 0, &threadID);

    CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WriteA, NULL, 0, &threadID);
    CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WriteB, NULL, 0, &threadID);
    CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WriteC, NULL, 0, &threadID);
    CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WriteD, NULL, 0, &threadID);

    ghMutex = CreateMutex(
        NULL,              // default security attributes
        FALSE,             // initially not owned
        NULL);             // unnamed mutex

    if (ghMutex == NULL)
    {
        printf("CreateMutex error: %d\n", GetLastError());
        return 1;
    }

    Sleep(60000);
    return 0;
}

 



 


作者: HarlanC

博客地址: http://www.cnblogs.com/harlanc/
个人博客: http://www.harlancn.me/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接

如果觉的博主写的可以,收到您的赞会是很大的动力,如果您觉的不好,您可以投反对票,但麻烦您留言写下问题在哪里,这样才能共同进步。谢谢!

你可能感兴趣的:(面试题-多线程编程)