C++0x has no semaphores? How to synchronize threads?

后端 未结 10 832
无人及你
无人及你 2020-11-22 07:18

Is it true that C++0x will come without semaphores? There are already some questions on Stack Overflow regarding the use of semaphores. I use them (posix semaphores) all the

相关标签:
10条回答
  • 2020-11-22 07:45

    C++20 will finally have semaphores - std::counting_semaphore<max_count>.

    These will have (at least) the following methods:

    • acquire() (blocking)
    • try_acquire() (non-blocking, returns immediatel)
    • try_acquire_for() (non-blocking, takes a duration)
    • try_acquire_until() (non-blocking, takes a time at which to stop trying)
    • release()

    This isn't listed on cppreference yet, but you can read these CppCon 2019 presentation slides, or watch the video. There's also the official proposal P0514R4, but I'm not sure that's the most up-to-date version.

    0 讨论(0)
  • 2020-11-22 07:45

    Also can be useful RAII semaphore wrapper in threads:

    class ScopedSemaphore
    {
    public:
        explicit ScopedSemaphore(Semaphore& sem) : m_Semaphore(sem) { m_Semaphore.Wait(); }
        ScopedSemaphore(const ScopedSemaphore&) = delete;
        ~ScopedSemaphore() { m_Semaphore.Notify(); }
    
       ScopedSemaphore& operator=(const ScopedSemaphore&) = delete;
    
    private:
        Semaphore& m_Semaphore;
    };
    

    Usage example in multithread app:

    boost::ptr_vector<std::thread> threads;
    Semaphore semaphore;
    
    for (...)
    {
        ...
        auto t = new std::thread([..., &semaphore]
        {
            ScopedSemaphore scopedSemaphore(semaphore);
            ...
        }
        );
        threads.push_back(t);
    }
    
    for (auto& t : threads)
        t.join();
    
    0 讨论(0)
  • 2020-11-22 07:46

    Based on Maxim Yegorushkin's answer, I tried to make the example in C++11 style.

    #include <mutex>
    #include <condition_variable>
    
    class Semaphore {
    public:
        Semaphore (int count_ = 0)
            : count(count_) {}
    
        inline void notify()
        {
            std::unique_lock<std::mutex> lock(mtx);
            count++;
            cv.notify_one();
        }
    
        inline void wait()
        {
            std::unique_lock<std::mutex> lock(mtx);
    
            while(count == 0){
                cv.wait(lock);
            }
            count--;
        }
    
    private:
        std::mutex mtx;
        std::condition_variable cv;
        int count;
    };
    
    0 讨论(0)
  • 2020-11-22 07:51

    You can work with mutex and condition variables. You gain exclusive access with the mutex, check whether you want to continue or need to wait for the other end. If you need to wait, you wait in a condition. When the other thread determines that you can continue, it signals the condition.

    There is a short example in the boost::thread library that you can most probably just copy (the C++0x and boost thread libs are very similar).

    0 讨论(0)
提交回复
热议问题