Thread: How do I make this notify_one choose a random thread?

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    2

    How do I make this notify_one choose a random thread?

    Code:
    #include <iostream>
    #include <thread>
    #include <memory>
    #include <chrono>
    #include <condition_variable>
    int o = rand() % 5;
    std::condition_variable* cv = new std::condition_variable();
    std::mutex cv_m;
    
    
    void threadFunc(std::shared_ptr<bool> flag2, int id)
    {
        while (true)
        {
            if (*flag2) std::cout << o;
            bool ifShouldPrint = (o == id);
            std::unique_lock<std::mutex> lock(cv_m);
            cv->wait(lock, ifShouldPrint(true);
            if (*flag2) std::cout << "Thread" << " " << id << std::endl;
        }
    }
    int main() {
    
    
        std::shared_ptr<bool> f2 = std::make_shared<bool>(false);
    
    
        std::thread threads[6];
    
    
        for (int i = 0; i < 6; i++)
            threads[i] = std::thread(threadFunc, f2, i);
        *f2 = true;
    
    
        while (true)
        {
            o = rand() % 5;
            cv->notify_all();
            std::this_thread::sleep_for(std::chrono::seconds(2));
        }
    
    
        return 0;
    }
    I want the program to choose a random thread each 2 seconds and make it print its number. Trying to implement it like this:
    - Main cycle generates a random number and notifies all threads
    - All threads compare their numbers to the generated one
    - The thread the number of which is equal to the generated one unlocks and prints its number
    But it gives an error in the mutex file, because of string 17 (cv->wait), because I don't really understand how to lock a thread with a condition. How do I make it work?
    Thanks in advance for any help.

  2. #2
    Registered User
    Join Date
    Oct 2019
    Posts
    2
    *I meant notify_all in the title

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I guess the first problem is what you're trying to do here, with the unbalanced parentheses and all.
    > cv->wait(lock, ifShouldPrint(true);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 03-02-2016, 10:28 AM
  2. How Do I Make This Container Thread-Safe?
    By MutantJohn in forum C++ Programming
    Replies: 47
    Last Post: 11-29-2015, 10:56 AM
  3. boost::thread and local random variables
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 01-05-2011, 09:05 AM
  4. How to make your Thread HOT
    By year2038bug in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 08-30-2005, 06:20 AM
  5. How to make a thread sleep or std::recv timeout?
    By BrianK in forum Linux Programming
    Replies: 3
    Last Post: 02-26-2003, 10:27 PM

Tags for this Thread