0

i've been wondering about it for a while now but never found any answers. is it possible to use something like a condition variable without a lock?

I have a vector of objects, and a thread pool, and I want each thread to access a specific cell once than go to wait for the master thread (that currently waits) to "broadcast" for another access. as far as i know the vector access is thread-safe, and since all threads go to wait for broadcast before the master thread comes, there is no real need for a lock there, yet pthread_cond requires a lock, and i found no other implementation for a conditional wait with no lock.

so it there an implementation in c++ using pthreads that waits on a condition without locks and also without spinning? Thanks.

[of course i could use a lock but then i lose the benefit of parallelism. another solution is having a mutex for each thread and have an array of mutexes at the master thread, but that seems like a lot of overhead.]

  • Look up "Lock-Free Data Structures." See also the LMAX Disruptor. – Robert Harvey Jun 1 at 15:44
  • Are you looking for a "barrier" instead? – rwong Jun 2 at 21:32
  • How does using a lock lose the benefit of parallelism? If all of your threads are waiting for something to happen before proceeding (great use for a barrier, BTW), there's no parallelism to be lost because they're all idle anyway. – Blrfl Jun 3 at 2:31
  • @Blrfl a single lock makes a serial section and they all need to pass it one by one as far as i understand. i don't mind using a lock, i'm basically asking if there's a lock that doesn't induce serialization, that multiple threads can pass in parallel. – Alon Hershkovitz Jun 6 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.