Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <Windows.h>
- #include <conio.h>
- #define SIZE 200
- #define BIG 8000
- volatile char queue[SIZE];
- int j = 0;
- CRITICAL_SECTION critsection;
- DWORD WINAPI th1(void *p)
- {
- while (1)
- {
- EnterCriticalSection(&critsection);
- queue[++j] = 'K';
- if (j > SIZE ) j = 0;
- for (int k = 0; k < BIG; k++) {}
- LeaveCriticalSection(&critsection);
- }
- }
- DWORD WINAPI th2(void *p)
- {
- while (1)
- {
- EnterCriticalSection(&critsection);
- queue[++j] = 'X';
- if (j > SIZE ) j = 1;
- for(int k = 0 ; k < BIG ; k++) { }
- LeaveCriticalSection(&critsection);
- }
- }
- int main()
- {
- InitializeCriticalSection(&critsection);
- HANDLE H1 = CreateThread(0, 0, th1, 0, 0, 0);
- HANDLE H2 = CreateThread(0, 0, th2, 0, 0, 0);
- while (!_kbhit())
- {
- for (int i = 0; i < SIZE; i++)
- printf("%c,", queue[i]);
- printf("\n");
- Sleep(1000);
- }
- CloseHandle(H1);
- CloseHandle(H2);
- DeleteCriticalSection(&critsection);
- return 0;
- }
- /* Wydruk
- ,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,
- K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,
- K,K,K,K,K,K,K,K,K,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,
- X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,
- X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,K,K,K,K,K,K,K,K,K,K,K,
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement