/****************************************************************************** *题目: 编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID * 在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC…依次递推。 *******************************************************************************/
my_typedef.h
#ifndef MY_TYPEDEF_H #define MY_TYPEDEF_H typedef char CHAR; typedef signed char INT8; typedef signed short INT16; typedef signed int INT32; //typedef signed long LONG64; typedef unsigned char UINT8; typedef unsigned short UINT16; typedef unsigned int UINT32; //typedef unsigned long ULONG64; typedef float FLOAT; typedef double DOUBLE; #define OSM_Strncpy strncpy #define OSM_Strncmp strncmp #define OSM_Strncat strncat #define OSM_Printf printf #define OSM_Getchar getchar #define OSM_Scanf scanf #endif
main.c
/****************************************************************************** *题目: 编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID * 在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC…依次递推。 *******************************************************************************/ #define WIN32_LEAN_AND_MEAN #include <stdio.h> #include <Windows.h> #include "my_typedef.h" DWORD WINAPI MyProc1( /* 线程函数 */ LPVOID lpParam ); DWORD WINAPI MyProc2( LPVOID lpParam ); DWORD WINAPI MyProc3( LPVOID lpParam ); HANDLE tHandleThread[3] = {NULL, NULL, NULL}; /* 线程句柄 */ HANDLE tHandleEvent[3] = {NULL, NULL, NULL}; /* 事件对象句柄 */ DWORD pThreadId[3] = {0, 0, 0}; /* 线程ID */ INT32 i32DoNoting = 0; INT32 main( void ) { INT32 i32I = 0; /* 定义了3个事件对象,并保存其句柄 */ tHandleEvent[0] = CreateEvent( NULL, TRUE, FALSE, NULL ); tHandleEvent[1] = CreateEvent( NULL, TRUE, TRUE, NULL ); tHandleEvent[2] = CreateEvent( NULL, TRUE, TRUE, NULL ); /* 创建了3个线程,并保存线程句柄 */ tHandleThread[0] = _beginthreadex( NULL, 0, MyProc1, NULL, 0, &pThreadId[0] ); tHandleThread[1] = _beginthreadex( NULL, 0, MyProc2, NULL, 0, &pThreadId[1] ); tHandleThread[2] = _beginthreadex( NULL, 0, MyProc3, NULL, 0, &pThreadId[2] ); /* 等待3个线程执行完毕,即每个线程打 */ WaitForMultipleObjects(3UL, tHandleThread, TRUE, INFINITE); /* 关闭事件对象 */ for (i32I = 0; i32I < 3; ++i32I) { i32DoNoting = CloseHandle(tHandleEvent[i32I]); } /* 关闭线程 */ for (i32I = 0; i32I < 3; ++i32I) { i32DoNoting = CloseHandle(tHandleThread[i32I]); } system("pause"); return (0); } DWORD WINAPI MyProc1( LPVOID lpParam ) { INT32 i32I = 0; for(; i32I < 10; ++i32I) { WaitForMultipleObjects(2UL, &tHandleEvent[1], TRUE, INFINITE); /* 等待第二个和第三个线程各打印一次自己的线程ID */ OSM_Printf("%d ", pThreadId[0]); /* 打印一次该线程ID */ i32DoNoting = ResetEvent(tHandleEvent[1]); /* 将事件对象重置为未激活状态 */ i32DoNoting = SetEvent(tHandleEvent[0]); /* 将事件对象变成激发状态 */ } return (0UL); } DWORD WINAPI MyProc2( LPVOID lpParam ) { INT32 i32I = 0; HANDLE tHandleTempEvent[2]; tHandleTempEvent[0] = tHandleEvent[0]; tHandleTempEvent[1] = tHandleEvent[2]; for(; i32I < 10; ++i32I) { WaitForMultipleObjects(2UL, tHandleTempEvent, TRUE, INFINITE); /* 等待第一个和第三个线程各打印一次自己的线程ID */ OSM_Printf("%d ", pThreadId[1]); i32DoNoting = ResetEvent(tHandleEvent[2]); i32DoNoting = SetEvent(tHandleEvent[1]); } return (0UL); } DWORD WINAPI MyProc3( LPVOID lpParam ) { INT32 i32I = 0; for(; i32I < 10; ++i32I) { WaitForMultipleObjects(2UL, tHandleEvent, TRUE, INFINITE); /* 等待第一个和第二个线程各打印一次自己的线程ID */ OSM_Printf("%d ", pThreadId[2]); i32DoNoting = ResetEvent(tHandleEvent[0]); i32DoNoting = SetEvent(tHandleEvent[2]); } return (0UL); }
程序截图:
作者:http://blog.csdn.net/lp310018931
时间: 2024-10-13 21:39:48