https://bugs.kde.org/show_bug.cgi?id=392331

Jens-W. Schicke-Uffmann <drahf...@gmx.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |drahf...@gmx.de

--- Comment #3 from Jens-W. Schicke-Uffmann <drahf...@gmx.de> ---
I have a reproducer for this warning, which also suggests is spurious (given
the source of the example):

Taking the example from
https://en.cppreference.com/w/cpp/thread/condition_variable:

#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include <condition_variable>

std::mutex m;
std::condition_variable cv;
std::string data;
bool ready = false;
bool processed = false;

void worker_thread()
{
    // Wait until main() sends data
    std::unique_lock lk(m);
    cv.wait(lk, []{return ready;});

    // after the wait, we own the lock.
    std::cout << "Worker thread is processing data\n";
    data += " after processing";

    // Send data back to main()
    processed = true;
    std::cout << "Worker thread signals data processing completed\n";

    // Manual unlocking is done before notifying, to avoid waking up
    // the waiting thread only to block again (see notify_one for details)
    lk.unlock();
    cv.notify_one();
}

int main()
{
    std::thread worker(worker_thread);

    data = "Example data";
    // send data to the worker thread
    {
        std::lock_guard lk(m);
        ready = true;
        std::cout << "main() signals data ready for processing\n";
    }
    cv.notify_one();

    // wait for the worker
    {
        std::unique_lock lk(m);
        cv.wait(lk, []{return processed;});
    }
    std::cout << "Back in main(), data = " << data << '\n';

    worker.join();
}

... compiling and running via
% g++-11 -O4 -ggdb test.c++ -lpthread
% valgrind --error-exitcode=1 --exit-on-first-error=yes --tool=helgrind ./a.out
... gives:
[...]
==30543== ---Thread-Announcement------------------------------------------
==30543== 
==30543== Thread #1 is the program's root thread
==30543== 
==30543== ----------------------------------------------------------------
==30543== 
==30543== Thread #1: pthread_cond_{signal,broadcast}: dubious: associated lock
is not held by any thread
==30543==    at 0x4847EF6: ??? (in
/usr/libexec/valgrind/vgpreload_helgrind-amd64-linux.so)
==30543==    by 0x10A241: main (test2.c++:44)

-- 
You are receiving this mail because:
You are watching all bug changes.

Reply via email to