Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- #include <conio.h>
- #include <windows.h>
- // -- Macros --
- //#define clear system("cls")
- // -- Global Variables --
- static bool s_Enter = false;
- // -- Namespaces --
- using namespace std;
- // -- Class & Functions --
- class Workers {
- public:
- void background_job(int&& n)
- {
- using namespace literals::chrono_literals;
- while (!s_Enter)
- {
- cout << "First Function " << n << endl;
- n++;
- this_thread::sleep_for(50ms);
- }
- }
- void checkEnd()
- {
- while (!s_Enter)
- if (_kbhit()) {
- s_Enter = true;
- _getch();
- }
- }
- };
- int main()
- {
- int ad = 0;
- // Thread header
- cout << "Threads started the job\n\n";
- // Declaring threads
- thread child(&Workers::background_job, Workers(), ad);
- thread child2(&Workers::checkEnd, Workers());
- // Childs finishing their jobs
- // ..
- child.join();
- child2.join();
- // Thread bottom
- cout << "\nThreads finished the job " << ad << endl;
- // Exit program
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement