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

5
  • Look up "Lock-Free Data Structures." See also the LMAX Disruptor. Commented Jun 1, 2019 at 15:44
  • Are you looking for a "barrier" instead? Commented Jun 2, 2019 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. Commented Jun 3, 2019 at 2: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. Commented Jun 6, 2019 at 20:08
  • What about a reader writer lock stackoverflow.com/questions/244316/reader-writer-locks-in-c stackoverflow.com/questions/31622059/… Commented Aug 26, 2021 at 0:26

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.