1418 Jobs (8) Multithreading open 3 threads, the IDs of 3 threads are a, B, C, each thread prints its own ID on the screen 10 times

Source: Internet
Author: User

/******************************************************************************* Title: Write a program to open 3 threads, the IDs of the 3 threads are a, B, C, each thread will print its own id*   on the screen 10 times, require the output must be displayed in the order of ABC, such as: Abcabc ... Recursion in turn. *******************************************************************************/


My_typedef.h

#ifndef my_typedef_h#define My_typedef_htypedef Char Char;typedef signed char INT8;      typedef signed short INT16;                                                 typedefsigned int Int32;//typedef signed longlong64;typedefunsigned char UINT8;                                               typedefunsigned short UINT16;                                                 typedefunsigned int uint32;//typedef unsigned long ulong64;typedeffloat FLOAT;                                                 Typedefdouble 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

/******************************************************************************* Title: Write a program to open 3 threads, the IDs of the 3 threads are a, B, C, each thread will print its own id* on the screen 10 times, require the output must be displayed in the order of ABC, such as: Abcabc ... Recursion in turn. /#define Win32_lean_and_mean# Include <stdio.h> #include <Windows.h> #include "my_typedef.h" DWORD WINAPI MyProc1 (/* thread function */lpvoid lpparam) ;D word WINAPI MyProc2 (lpvoid lpparam);D word WINAPI MyProc3 (lpvoid lpparam);      HANDLE Thandlethread[3] = {null, NULL, null};/* thread handle */handle thandleevent[3] = {null, NULL, null};/* event object handle */dword Pthreadid[3] = {0, 0, 0};/* thread id */int32i32donoting = 0;int32 main (void) {INT32 i32i = 0;/* defines 3 event objects and saves their handles */thandleeven T[0] = CreateEvent (null, TRUE, FALSE, NULL); THANDLEEVENT[1] = CreateEvent (null, True, true, null), thandleevent[2] = CreateEvent (null, True, true, NULL),/* Creates 3 threads, and Save thread handle */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])///* Wait for 3 threads to execute, i.e. each thread hits */waitformultipleobjects (3UL, Thandlethread, TRUE, INFINITE);/* Close Event object */for (i32i = 0; I32i < 3; ++i32i) {i32donoting = CloseHandle (Thandleevent[i32i]);} /* Close thread */for (i32i = 0; i32i < 3; ++i32i) {i32donoting = CloseHandle (Thandlethread[i32i]);} System ("pause"); return (0);} DWORD WINAPI MyProc1 (lpvoid lpparam) {INT32 i32i = 0;for (; i32i < ++i32i) {waitformultipleobjects (2UL, &thandlee Vent[1], TRUE, INFINITE);/* waits for the second and third threads to print their own thread ID */osm_printf ("%d", pthreadid[0]);/* Print this thread id once */i32donoting = ResetEvent (thandleevent[1]);/* Resets the event object to an inactive state */i32donoting = SetEvent (thandleevent[0]);/* Turns the event object into a firing state */}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);/* waits for the first and third threads to print their own thread ID */osm_printf ("%d", pthreadid[1]) at a time; i32donoting = ResetEvent (thandleevent[2]); i32donoting = SetEvent (thandleevent[1]);} return (0UL);}  DWORD WINAPI MyProc3 (lpvoid lpparam) {INT32 i32i = 0;for (; i32i < ++i32i) {waitformultipleobjects (2UL, Thandleevent, TRUE, INFINITE);/* waits for the first and second threads to print their own thread ID */osm_printf ("%d", pthreadid[2]); i32donoting = ResetEvent (thandleevent[0) ); i32donoting = SetEvent (thandleevent[2]);} return (0UL);}

Program:



Author: http://blog.csdn.net/lp310018931

1418 Jobs (8) Multithreading open 3 threads, the IDs of 3 threads are a, B, C, each thread prints its own ID on the screen 10 times

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.