相关疑难解决方法(0)

如何在c ++中使用/创建unique_lock?

请问,任何人都可以解释如何在c ++中使用和创建unique_lock吗?它应该被用来既能获得互斥到显示器的任何程序,并能在条件变量进行等待()...我不是从文档了解我应该如何创建它.是必要的互斥?这是一个伪代码:

/* compile with g++, flags -std=c++0x -lpthread */

#include <condition_variable>
#include <mutex>
#include <thread>
#include <iostream>
#include <string.h>
#include <unistd.h>

class monitorTh {

private:

    std::mutex m;
    std::condition_variable waitP;
    std::condition_variable waitC;
    char element[32];
    std::unique_lock::unique_lock l;

public:
    void produce(char* elemProd) {
        l.lock();
        if (/*already_present_element*/) {
            waitP.wait(l);
        }
        else {/*produce element*/}
        l.unlock();
    }

    void consume() {
        /*something specular*/
    }
};

int main(int argc, char* argv[]) {

    monitorTh* monitor = new monitorTh();
    char prodotto[32] = "oggetto";

    std::thread producer([&]() {
        monitor->produce(prodotto);
    });

    std::thread …
Run Code Online (Sandbox Code Playgroud)

c++ locking c++11

33
推荐指数
3
解决办法
3万
查看次数

标签 统计

c++ ×1

c++11 ×1

locking ×1