Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- #include <chrono>
- #include <mutex>
- using namespace std;
- /*
- Due thread che non fanno altro che aspettare un po` di millisecondi ciascuno
- */
- mutex m; //la mutex di controllo
- const int MAX_ITERAZIONI = 120;
- int conta = 0;
- void funzione_thread1()
- {
- while (conta < MAX_ITERAZIONI)
- {
- m.lock();
- conta++;
- this_thread::sleep_for(chrono::milliseconds(50));
- m.unlock();
- }
- }
- void funzione_thread2()
- {
- while (conta < MAX_ITERAZIONI)
- {
- m.lock();
- conta++;
- this_thread::sleep_for(chrono::milliseconds(50));
- m.unlock();
- }
- }
- int main()
- {
- //cronometro il tempo totale ...
- auto t1 = chrono::high_resolution_clock::now();
- thread thread_1(funzione_thread1);
- thread thread_2(funzione_thread1);
- thread_1.join();
- thread_2.join();
- auto t2 = chrono::high_resolution_clock::now();
- auto int_ms = chrono::duration_cast<chrono::milliseconds>(t2 - t1);
- cout << "Durata: " << int_ms.count() << " ms\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment