'wait and notify in C/C++ shared memory

How to wait and notify like in Java In C/C++ for shared memory between two or more thread?I use pthread library.



Solution 1:[1]

In your title, you blend C and C++ together so casually into "C/C++". I hope, you are not writing a program that is a mixture of the two.

If you are using C++11, you will find a portable alternative to pthreads (on POSIX systems, it usually uses pthreads under the hood though).

You can use std::condition_variable + std::mutex for wait/notify. This example shows how:

#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include <condition_variable>
 
std::mutex m;
std::condition_variable cv;
std::string data;
bool mainReady = false;
bool workerReader = false;
 
void worker_thread()
{
    // Wait until main() sends data
    {
        std::unique_lock<std::mutex> lk(m);
        cv.wait(lk, []{return mainReady;});
    }
 
    std::cout << "Worker thread is processing data: " << data << std::endl;
    data += " after processing";
 
    // Send data back to main()
    {
        std::lock_guard<std::mutex> lk(m);
        workerReady = true;
        std::cout << "Worker thread signals data processing completed\n";
    }
    cv.notify_one();
}
 
int main()
{
    std::thread worker(worker_thread);
 
    data = "Example data";
    // send data to the worker thread
    {
        std::lock_guard<std::mutex> lk(m);
        mainReady = true;
        std::cout << "main() signals data ready for processing\n";
    }
    cv.notify_one();
 
    // wait for the worker
    {
        std::unique_lock<std::mutex> lk(m);
        cv.wait(lk, []{return workerReady;});
    }
    std::cout << "Back in main(), data = " << data << '\n';
 

    // wait until worker dies finishes execution
    worker.join();
}

Solution 2:[2]

pthread_cond_wait and pthread_cond_signal can be used to synchronize based on a condition

Solution 3:[3]

Using Condition Variables is one way to do it: those are available when using the pthread library under Linux (see link).

A condition variable is a variable of type pthread_cond_t and is used with the appropriate functions for waiting and later, process continuation.

Solution 4:[4]

If you do not care about portability, Linux offers eventfd, which gives you exactly what you want. Each eventfd keeps an internal counter. In the default mode, reading from the eventfd blocks if the counter is zero, otherwise returns immediately. Writing to it will add to the internal counter.

The wait call would thus just be a uint64_t buf_a; read(event_fd, &buf_a, sizeof(buf_a));, where buf must be an 8-byte buffer. To notify the waiting thread, you would do uint64_t buf_b = 1; write(event_fd, &buf_b, sizeof(buf_b));.

Solution 5:[5]

If available, you might use POSIX semaphores. The pthread library has mutexes, which might work for you as well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 jspcal
Solution 3 jldupont
Solution 4 Julian Stecklina
Solution 5 Brad Larson