0

UPDATE:
It seems to me like my question is not duplicate. My accent on that nuance - we do not need to transfer data. And actually my question - is three different questions.

I have a few questions.
1) std::condition_variable is everywhere considered as a means of synchronizing access to data. Thus, examples of usage usually look like this:

void put (const T &val) {
  std::unique_lock<std::mutex> lock(m_Mtx);
  m_Value = val;
  m_CondVar.notify_one();
}
T get () {
  std::unique_lock<std::mutex> lock(m_Mtx);
  m_CondVar.wait(lock);
  return m_Value;
}

It's clear. But what if I do not need to transfer data, but only need to wake up the waiting thread (if there is one) - can I, in this case, NOT use the mutex and use code like this?

void put_nolock () {
  m_CondVar.notify_one();
}

It is quite not clear from documentation.In the documentation for pthread_cond_signal can see:

The pthread_cond_broadcast() or pthread_cond_signal() functions may be called by a thread whether or not it currently owns the mutex that threads calling pthread_cond_wait() or pthread_cond_timedwait() have associated with the condition variable during their waits; however, if predictable scheduling behavior is required, then that mutex shall be locked by the thread calling pthread_cond_broadcast() or pthread_cond_signal().

I can not realize - what exactly does this mean ("however, if predictable scheduling behavior is required...")? My goal is micro-optimization, less mutexes, less potential context switching. So maybe it does not make sense at all and the cost of using mutex + cond_var is equal the cost of using just cond_var?

2) what is the cost of m_CondVar.notify_one()? Will it be a regular syscall? Does it depends on whether there are waiting threads or that cond_var or not?

3) maybe you know more lightweight way to notify (without or with sending a message, it does not matter) one thread from another?

o2gy
  • 333
  • 1
  • 13
  • You have to lock even if the condition is atomic. – François Andrieux Jan 16 '19 at 21:36
  • Your condition variable doesn't actually have a condition to wait on. It's just waiting for an event. Due to spurious waking it may not behave as you intend. – François Andrieux Jan 16 '19 at 21:37
  • Do not use condition variables as a general signalling mechanism - they are not designed for this. If you find it unfair, find small consolation in the fact that you are not alone here. – SergeyA Jan 16 '19 at 21:39
  • Re, "what if I do not need to transfer data, but only need to wake up the waiting thread?" That doesn't make any sense. If nothing has changed in memory, then there can be no reason to wake the waiting thread. If something _has_ changed, then your program must use some form of _synchronization_ (e.g., by proper use of a `std::mutex`) to ensure that the change will be visible to the other thread when it wakes up. – Solomon Slow Jan 16 '19 at 21:47
  • @SolomonSlow, well, that is right. But lets imagine I have queue which I can't modify, but I can to know if there is new element in that queue, so I just try to workaround this with minimal cost. – o2gy Jan 16 '19 at 21:56
  • @SergeyA, What would you advise to use instead? – o2gy Jan 16 '19 at 21:57
  • 1
    @o2gy nothing in posix has a signal semantic. In Linux there is linux-specific event_fd. – SergeyA Jan 16 '19 at 22:13
  • @FrançoisAndrieux it seems to me like my question is not duplicate. My accent on that - I do not need to transfer data. And actually my question - is three different questions. – o2gy Jan 17 '19 at 10:14
  • @o2gy If there is a new element in the queue, then something has changed in memory. If something has changed in memory, then the threads need to use some form of synchronization (e.g., a mutex) to ensure that the change will be visible to those threads that need to see it. – Solomon Slow Jan 17 '19 at 14:35
  • 2
    @o2gy This is why asking multiple questions in a single question [is not allowed](https://meta.stackoverflow.com/questions/371614/why-isnt-it-good-to-ask-multiple-questions-and-answers-in-one-question). Assuming the title of your question is what you actually want to know, this question can be closed as a duplicate. At least it something comes of it. If the question can't be boiled down to a single query then [it should be closed](https://meta.stackoverflow.com/questions/267058/how-to-handle-a-question-that-asks-many-things) for being too broad. – François Andrieux Jan 17 '19 at 14:59
  • @o2gy If the duplicate does not answer your question, you may have a more fundamental misunderstanding of what `std::condition_variable` actually does. It allows you to wait for a condition but it is not itself a condition. It requires a separate condition (a state to occur) and requires that condition to be synchronized by a mutex even if it would otherwise be atomic. In the example you gave about queues, the condition would be that the queue is not empty and a mutex would be required when modifying that condition. – François Andrieux Jan 17 '19 at 15:01
  • This tutorial (https://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html) explains how to use the condition variables provided by the Java standard library. It's a different library, and it's written in a different language, but the ideas are nearly identical. – Solomon Slow Jan 17 '19 at 15:38

0 Answers0