Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <atomic>
- #include <mutex>
- #include <thread>
- #include <windows.h>
- #include <stdio.h>
- #include <conio.h>
- #include <tchar.h>
- #include <iostream>
- #include <list>
- #include <chrono>
- #include <stdlib.h> /* srand, rand */
- #include <time.h>
- #include <vector>
- using namespace std;
- CRITICAL_SECTION csS;
- CRITICAL_SECTION csR;
- void threadA(list<int> &S)
- {
- int i = 0;
- while(1)
- {
- EnterCriticalSection(&csS);
- S.push_back(i++);
- cout << S.back();
- LeaveCriticalSection(&csS);
- }
- }
- void threadB(list<int> &S,list<double> &R)
- {
- while(1)
- {
- EnterCriticalSection(&csS);
- if (!S.empty())
- {
- double a = S.back()*S.back();
- EnterCriticalSection(&csR);
- R.push_back(a);
- LeaveCriticalSection(&csR);
- }
- else
- Sleep(1000);
- LeaveCriticalSection(&csS);
- }
- }
- void threadC(list<int> &S,list<double> &R)
- {
- while(1)
- {
- EnterCriticalSection(&csS);
- if (!S.empty())
- {
- double a = S.back()/3;
- EnterCriticalSection(&csR);
- R.push_back(a);
- LeaveCriticalSection(&csR);
- }
- else
- Sleep(1000);
- LeaveCriticalSection(&csS);
- }
- }
- void threadD(list<double> &R)
- {
- while(1)
- {
- EnterCriticalSection(&csR);
- if (!R.empty())
- {
- cout << R.back() << endl;
- }
- else
- {
- cout << "R is empty\n";
- Sleep(1000);
- }
- LeaveCriticalSection(&csR);
- }
- }
- int main()
- {
- list<int> S;
- list<int> R;
- HANDLE hThr[4];
- unsigned long uThrID;
- DWORD dwEvent;
- InitializeCriticalSection(&csS);
- InitializeCriticalSection(&csR);
- hThr[0] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)threadA,NULL,0,&uThrID);
- cout << "A\n";
- hThr[1] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)threadB,NULL,0,&uThrID);
- cout << "B\n";
- hThr[2] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)threadC,NULL,0,&uThrID);
- cout << "C\n";
- hThr[3] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)threadD,NULL,0,&uThrID);
- cout << "D\n";
- dwEvent = WaitForMultipleObjects(4, hThr, true, INFINITE);
- cout << "Hello world!" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement