Mutex & Lock

C++11 多了一些 mutex 的 wrapper 方便程式可以符合 RAII 的形式。


std::mutex

多使用std::mutex不要再繼續使用C的pthread_mutex_t

Example, bad
void fn()
{
    pthread_mutex_t mutex;

    pthread_mutex_init(&mutex, NULL);  // Bad, caller 初始化資源

    pthread_mutex_lock(&mutex);

    // ... critical section ...

    pthread_mutex_unlock(&mutex);

    pthread_mutex_destroy(&mutex);     // Bad, caller 清除資源
}
Example, good
void fn()
{
    std::mutex mutex;            // RAII-styled mutex

    mutex.lock();

    // ... critical section ...

    mutex.unlock();
}

std::lock_guard

mutex 的 wrapper,在不使用的時候會 unlock。

Example
void bad()
{
    std::mutex mutex;

    mutex.lock();

    // ... critical section ...

    mutex.unlock();                // Bad, 可能不會呼叫
}

void good()
{
    std::mutex mutex;

    std::lock_guard<std::mutex> lock(mutex);

    // ... critical section ...

} // Good, 離開 scope 的時候 mutex 會被 unlock()

std::lock_guard並沒有提供unlock(),如果要的話可以使用std::unique_lock


Reference

results matching ""

    No results matching ""