Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <stdio.h>
- CRITICAL_SECTION criticalSection;
- long int zmienna = 1;
- unsigned long noOfEntrance1;
- unsigned long noOfEntrance2;
- unsigned long CompletedEntrance = 0;
- unsigned long AbortiveEntrance = 0;
- unsigned long __stdcall thread (long* v);
- int main(int argc, char* argv[])
- {
- InitializeCriticalSection(&criticalSection);
- HANDLE hw1;
- hw1 = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)thread, &noOfEntrance1, 0, 0);
- HANDLE hw2;
- hw2 = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)thread, &noOfEntrance2, 0, 0);
- while(1)
- {
- printf("%ld\t%ld\t%ld\t%ld\t%ld\n", zmienna, noOfEntrance1, noOfEntrance2, CompletedEntrance, AbortiveEntrance);
- Sleep(200);
- }
- CloseHandle(hw1);
- CloseHandle(hw2);
- DeleteCriticalSection(&criticalSection);
- return 0;
- }
- unsigned long __stdcall thread (long* v)
- {
- //EnterCriticalSection(&criticalSection);
- while(1)
- {
- //EnterCriticalSection(&criticalSection);
- //EnterCriticalSection(&criticalSection);
- if(TryEnterCriticalSection(&criticalSection))
- {
- zmienna += 100;
- zmienna *= 200;
- zmienna /= 200;
- zmienna -= 100;
- (*v)++;
- CompletedEntrance++;
- LeaveCriticalSection(&criticalSection);
- }
- else
- {
- AbortiveEntrance++;
- Sleep(20);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement