Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // os-p0103.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <Windows.h>
- #include <process.h>
- int d = 0;
- //void _cdecl function(void*);
- unsigned _stdcall function(void*);
- CRITICAL_SECTION CriticalSection;
- int _tmain(int argc, _TCHAR* argv[])
- {
- int x=1,y=2,z=3;
- unsigned int id[3];
- HANDLE hd[3];
- if (!InitializeCriticalSectionAndSpinCount(&CriticalSection,
- 0x00000400))
- return 0;
- hd[0] = (HANDLE)_beginthreadex(NULL, 0, &function, &x, 0, &id[0]);
- //EnterCriticalSection(&CriticalSection);
- hd[1] = (HANDLE)_beginthreadex(NULL, 0, &function, &y, 0, &id[1]);
- //EnterCriticalSection(&CriticalSection);
- hd[2] = (HANDLE)_beginthreadex(NULL, 0, &function, &z, 0, &id[2]);
- //LeaveCriticalSection(&CriticalSection);
- WaitForMultipleObjects(3,hd, TRUE, INFINITE);
- DeleteCriticalSection(&CriticalSection);
- return 0;
- }
- unsigned _stdcall function(void* k)
- {
- int* k1 = (int*)k;
- for (size_t i = 0; i < 10;)
- {
- EnterCriticalSection(&CriticalSection);
- if (*k1 == (d+1)){
- if (*k1<3)
- printf_s("%d", *k1);
- else
- {
- printf_s("%d\n", *k1);
- }
- d=(d+1)%3;
- i++;
- }
- LeaveCriticalSection(&CriticalSection);
- Sleep(200);
- }
- _endthread();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement