简体   繁体   English

如何将 std::condition_variable 与我自己的互斥体包装器一起使用

[英]How to use std::condition_variable with my own mutex wrapper

I wanted to demonstrate / log some std::mutex behaviour.我想演示/记录一些std::mutex行为。 I would like to log every call to lock() and unlock() (regardless whether they succeed or have to block).我想记录对lock()unlock()的每次调用(无论它们是成功还是必须阻止)。 Originally, since those methods are not virtual , I tried to wrap the standard std::mutex in the following way, instead of using inheritance:最初,由于这些方法不是virtual ,因此我尝试以以下方式包装标准std::mutex ,而不是使用 inheritance:

struct mutex_wrapper {
    std::mutex mut = std::mutex();

    void lock() {
        std::cout << "LOCKING MUTEX\n";
        mut.lock();
    }

    void unlock() {
        std::cout << "UNLOCKING MUTEX\n";
        mut.unlock();
    }
};

Ignore the fact that calls to std::cout << could produce mangled output when called within multiple threads.忽略在多个线程中调用std::cout <<可能会产生损坏的 output 的事实。

Then I wanted to inspect the output of a simple std::condition_variable example, as such:然后我想检查一个简单的std::condition_variable示例的 output,如下所示:

int main() {
    auto ready = false;
    auto mutex = mutex_wrapper();
    auto cvar = std::condition_variable();

    auto t = std::thread([&mutex, &cvar, &ready] {
        std::unique_lock lock(mutex);
        cvar.wait(lock, [&ready] { return ready; });
        std::cout << "woke up\n";
    });

    std::this_thread::sleep_for(std::chrono::seconds(2));
    {
        std::lock_guard lock(mutex);
        ready = true;
    }
    std::cout << "notifying\n";
    cvar.notify_one();

    t.join();
}

The problem is that it doesn't compile because, apparently, wait() requires that the corresponding std::unique_lock is instantiated with the regular std::mutex while I tried to use it with my own mutex_wrapper .问题是它不能编译,因为显然wait()要求相应的std::unique_lock用常规的std::mutex实例化,而我试图将它与我自己的mutex_wrapper一起使用。

Is there any way to log the internal usage of locking and unlocking of a standard mutex?有什么方法可以记录标准互斥锁的锁定和解锁的内部使用情况? Or is there a way to make std::condition_variable work with wrappers?或者有没有办法让std::condition_variable与包装器一起工作?

std::condition_variable only works specifically with std::unique_lock<std::mutex> -- so you'll probably want to usestd::condition_variable_any . std::condition_variable仅适用于std::unique_lock<std::mutex> ——因此您可能希望使用std::condition_variable_any This is a generalization of std::condition_variable that can work with any BasicLockable type, such as your mutex wrapper.这是std::condition_variable的概括,可以与任何BasicLockable类型一起使用,例如您的互斥体包装器。

Note that it may be a slight pessimization over using std::condition_variable / std::mutex directly since usually these are tightly coupled in the implementation for better performance.请注意,直接使用std::condition_variable / std::mutex可能有点悲观,因为通常它们在实现中紧密耦合以获得更好的性能。 But since you're logging here anyway, I suspect this won't be that much of an issue overall.但是,既然您无论如何都在这里登录,我怀疑这总体上不会是那么大的问题。

An alternative approach is to write your own custom condition_variable wrapper that operates directly in terms of your wrapped mutex type and an underlying std::condition_variable -- and extract the std::mutex directly when locking.另一种方法是编写您自己的自定义condition_variable包装器,该包装器直接根据您包装的mutex类型和底层std::condition_variable操作 - 并在锁定时直接提取std::mutex

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM