2

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.]

Deduplicator
  • 8,591
  • 5
  • 31
  • 50
  • Look up "Lock-Free Data Structures." See also the LMAX Disruptor. – Robert Harvey Jun 01 '19 at 15:44
  • Are you looking for a "barrier" instead? – rwong Jun 02 '19 at 21:32
  • 1
    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 03 '19 at 02:31
  • 1
    @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 06 '19 at 20:08
  • What about a reader writer lock https://stackoverflow.com/questions/244316/reader-writer-locks-in-c https://stackoverflow.com/questions/31622059/how-to-implement-a-reader-writer-lock-in-c14 – Jerry Jeremiah Aug 26 '21 at 00:26

0 Answers0