第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

C+0x沒有信號量?如何同步線程?

C+0x沒有信號量?如何同步線程?

C++ C
慕桂英3389331 2019-07-02 16:21:53
C+0x沒有信號量?如何同步線程?C+0x真的沒有信號量嗎?關(guān)于堆棧溢出,已經(jīng)有一些關(guān)于使用信號量的問題。我一直使用它們(POSIX信號量)讓線程在另一個線程中等待某個事件:void thread0(...){   doSomething0();   event1.wait();   ...}void thread1(...){   doSomething1();   event1.post();   ...}如果我用互斥物做這個的話:void thread0(...){   doSomething0();   event1.lock(); event1.unlock();   ...}void thread1(...){   event1.lock();   doSomethingth1();   event1.unlock();   ...}問題:它很難看,不能保證線程1先鎖互斥鎖(考慮到同一個線程應該鎖定和解鎖互斥對象,您也不能在線程0和線程1啟動之前鎖定事件1)。那么,既然Boost也沒有信號量,那么實現(xiàn)上述目標的最簡單方法是什么呢?
查看完整描述

3 回答

?
慕村225694

TA貢獻1880條經(jīng)驗 獲得超4個贊

您可以通過互斥和條件變量輕松地構(gòu)建一個:

#include <mutex>#include <condition_variable>class semaphore{private:
    std::mutex mutex_;
    std::condition_variable condition_;
    unsigned long count_ = 0; // Initialized as locked.public:
    void notify() {
        std::lock_guard<decltype(mutex_)> lock(mutex_);
        ++count_;
        condition_.notify_one();
    }

    void wait() {
        std::unique_lock<decltype(mutex_)> lock(mutex_);
        while(!count_) // Handle spurious wake-ups.
            condition_.wait(lock);
        --count_;
    }

    bool try_wait() {
        std::lock_guard<decltype(mutex_)> lock(mutex_);
        if(count_) {
            --count_;
            return true;
        }
        return false;
    }};


查看完整回答
反對 回復 2019-07-02
?
精慕HU

TA貢獻1845條經(jīng)驗 獲得超8個贊

我決定用我能寫的最健壯的/通用的C+11信號量,盡可能地按照標準的風格來寫(注意using semaphore = ...,你通常只會用這個名字semaphore類似于正常使用string不basic_string):


template <typename Mutex, typename CondVar>

class basic_semaphore {

public:

    using native_handle_type = typename CondVar::native_handle_type;


    explicit basic_semaphore(size_t count = 0);

    basic_semaphore(const basic_semaphore&) = delete;

    basic_semaphore(basic_semaphore&&) = delete;

    basic_semaphore& operator=(const basic_semaphore&) = delete;

    basic_semaphore& operator=(basic_semaphore&&) = delete;


    void notify();

    void wait();

    bool try_wait();

    template<class Rep, class Period>

    bool wait_for(const std::chrono::duration<Rep, Period>& d);

    template<class Clock, class Duration>

    bool wait_until(const std::chrono::time_point<Clock, Duration>& t);


    native_handle_type native_handle();


private:

    Mutex   mMutex;

    CondVar mCv;

    size_t  mCount;

};


using semaphore = basic_semaphore<std::mutex, std::condition_variable>;


template <typename Mutex, typename CondVar>

basic_semaphore<Mutex, CondVar>::basic_semaphore(size_t count)

    : mCount{count}

{}


template <typename Mutex, typename CondVar>

void basic_semaphore<Mutex, CondVar>::notify() {

    std::lock_guard<Mutex> lock{mMutex};

    ++mCount;

    mCv.notify_one();

}


template <typename Mutex, typename CondVar>

void basic_semaphore<Mutex, CondVar>::wait() {

    std::unique_lock<Mutex> lock{mMutex};

    mCv.wait(lock, [&]{ return mCount > 0; });

    --mCount;

}


template <typename Mutex, typename CondVar>

bool basic_semaphore<Mutex, CondVar>::try_wait() {

    std::lock_guard<Mutex> lock{mMutex};


    if (mCount > 0) {

        --mCount;

        return true;

    }


    return false;

}


template <typename Mutex, typename CondVar>

template<class Rep, class Period>

bool basic_semaphore<Mutex, CondVar>::wait_for(const std::chrono::duration<Rep, Period>& d) {

    std::unique_lock<Mutex> lock{mMutex};

    auto finished = mCv.wait_for(lock, d, [&]{ return mCount > 0; });


    if (finished)

        --mCount;


    return finished;

}


template <typename Mutex, typename CondVar>

template<class Clock, class Duration>

bool basic_semaphore<Mutex, CondVar>::wait_until(const std::chrono::time_point<Clock, Duration>& t) {

    std::unique_lock<Mutex> lock{mMutex};

    auto finished = mCv.wait_until(lock, t, [&]{ return mCount > 0; });


    if (finished)

        --mCount;


    return finished;

}


template <typename Mutex, typename CondVar>

typename basic_semaphore<Mutex, CondVar>::native_handle_type basic_semaphore<Mutex, CondVar>::native_handle() {

    return mCv.native_handle();

}


查看完整回答
反對 回復 2019-07-02
  • 3 回答
  • 0 關(guān)注
  • 600 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號