Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ossp-demo-20160928.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <process.h>
- #include <windows.h>
- void __cdecl my_thread(void *);
- CRITICAL_SECTION CriticalSection;
- int queue = 0;
- int _tmain(int argc, _TCHAR* argv[])
- {
- char a = 'A', b = 'B', c = 'C', d = 'D', e = 'E';
- HANDLE handles[5];
- // Initialize the critical section one time only.
- if (!InitializeCriticalSectionAndSpinCount(&CriticalSection,
- 0x00000400))
- return 0;
- //запустити my_thread('A');
- handles[0]= (HANDLE)_beginthread(my_thread, 0, &a);
- //запустити my_thread('B');
- handles[1] = (HANDLE)_beginthread(my_thread, 0, &b);
- //запустити my_thread('C');
- handles[2] = (HANDLE)_beginthread(my_thread, 0, &c);
- //запустити my_thread('D');
- handles[3] = (HANDLE)_beginthread(my_thread, 0, &d);
- //запустити my_thread('E');
- handles[4] = (HANDLE)_beginthread(my_thread, 0, &e);
- WaitForMultipleObjects(5, handles, TRUE, INFINITE);
- // Release resources used by the critical section object.
- DeleteCriticalSection(&CriticalSection);
- return 0;
- }
- void __cdecl my_thread(void *arg)
- {
- char *ch = (char *)arg;
- for (size_t i = 0; i < 10;)
- {
- // Request ownership of the critical section.
- EnterCriticalSection(&CriticalSection);
- if (queue == (*ch - 65))
- {
- putchar(*ch);
- queue = (queue + 1) % 5;
- i++;
- if (*ch == 'E')
- putchar('\n');
- }
- // Release ownership of the critical section.
- LeaveCriticalSection(&CriticalSection);
- Sleep(200);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement