zoukankan      html  css  js  c++  java
  • 迅雷笔试题

      编写一个程序,开启3个线程,这3个线程的ID分别为ABC,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC.依次递推。

    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>  
    #include <string>  
    #include <stack>  
    #include <windows.h>  
    #include <process.h>  
    using namespace std;  
    
    HANDLE hEventA;
    HANDLE hEventB;
    HANDLE hEventC;
    
    HANDLE hThreadA;
    HANDLE hThreadB;
    HANDLE hThreadC;
    
    UINT __stdcall ThreadA(LPVOID lparam)
    {
    	int n = 10;
    	while(n--)
    	{
    		WaitForSingleObject(hEventA, INFINITE);
    		printf("A");
    		ResetEvent(hEventA);
    		SetEvent(hEventB);
    	}
    	return 1;
    }
    
    UINT __stdcall ThreadB(LPVOID lparam)
    {
    	int n = 10;
    	while(n--)
    	{
    		WaitForSingleObject(hEventB, INFINITE);
    		printf("B");
    		ResetEvent(hEventB);
    		SetEvent(hEventC);
    	}
    	return 1;
    }
    
    UINT __stdcall ThreadC(LPVOID lparam)
    {
    	int n = 10;
    	while(n--)
    	{
    		WaitForSingleObject(hEventC, INFINITE);
    		printf("C\n");
    		ResetEvent(hEventC);
    		SetEvent(hEventA);
    	}
    	return 1;
    }
    
    int main()  
    {  
    	hEventA = CreateEvent(NULL, TRUE, FALSE, NULL);
    	hEventB = CreateEvent(NULL, TRUE, FALSE, NULL);
    	hEventC = CreateEvent(NULL, TRUE, FALSE, NULL);
    	SetEvent(hEventA);
    	hThreadA = (HANDLE)_beginthreadex(NULL, 0, ThreadA, NULL, 0, NULL);
    	hThreadB = (HANDLE)_beginthreadex(NULL, 0, ThreadB, NULL, 0, NULL);
    	hThreadC = (HANDLE)_beginthreadex(NULL, 0, ThreadC, NULL, 0, NULL);
    	WaitForSingleObject(hThreadA, INFINITE);
    	WaitForSingleObject(hThreadB, INFINITE);
    	WaitForSingleObject(hThreadC, INFINITE);
    	CloseHandle(hThreadA);
    	CloseHandle(hThreadB);
    	CloseHandle(hThreadC);
    	CloseHandle(hEventA);
    	CloseHandle(hEventB);
    	CloseHandle(hEventC);
    	return 0;  
    }  
    

      

  • 相关阅读:
    子网掩码
    一个正则表达式:该正则表达式标示了后面有数字,但又不能是某特定数字的情况
    C++:STL标准入门汇总
    SOAP
    uva10236The Fibonacci Primes
    uvalive3209City Game
    uvalive3695Distant Galaxy
    uva11549Calculator Conundrum
    uva11078Open Credit System
    uvalive3295Counting Triangles
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3099758.html
Copyright © 2011-2022 走看看