Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // exchanger.cpp
- // malnati
- //
- // Created by Elia Migliore on 19/07/2019.
- // Copyright © 2019 Elia Migliore. All rights reserved.
- //
- #include "exchanger.hpp"
- #include <iostream>
- #include <mutex>
- #include <thread>
- template <typename T>
- class Exchanger {
- T exchanged;
- bool is_exchanged;
- std::mutex m_protect_data;
- std::mutex m_protect_sleep;
- std::condition_variable cv;
- Exchanger(T& exchanged) {
- std::unique_lock<std::mutex> l(m_protect_data);
- if (is_exchanged){
- // cv.signal()
- std::swap(exchanged, this->exchanged);
- cv.notify_one();
- } else {
- // exchange the item and set to true the variable
- // that represent the exchanged flag
- this->exchanged = exchanged;
- is_exchanged = true;
- // release the lock for protecting the variable
- l.release();
- // wait for the other thread to wake me up when
- // another element has arrived and the exchanged
- // item is exchanged
- // unique lock per il paradisma RAII
- std::unique_lock<std::mutex> l_s(m_protect_sleep);
- cv.wait(l_s);
- // getting again te lock to protect the flag and data
- l.lock();
- exchanged = this->exchanged;
- is_exchanged = false; // restore the class for next use
- // now RAII paradigm release the second lock for me
- }
- // output to indicate the success
- std::cout << "element to exchange is: " << typeid(exchanged).name() << std::endl;
- // now RAII paradigm release the first lock for me
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement