0

Why should we use wait() and signal() operation in multithreading applications?

I'm relatively new to multithreading and somewhat understand mutual exclusion but I need a better understanding of how wait() and signal() come into the equation.

It seems I'm achieving thread safety by only using lock() and unlock(). Am I wrong?

Can someone give me an example of wait/signal being used and wait and signal not being used with lock/unlock? What are the benefits to using wait/signal over just lock/unlock?

Thanks.

TheIronChef
  • 81
  • 1
  • 4

2 Answers2

1

I work with computational maths/science so my examples come from there.

If you were doing a reduce operation such as a dot product (need to sum many calculations) then lock and unlock are useful as the order of the sum does not matter and if it's free the thread should go for it.

If you were solving a PDE over time before you can take the next time step the previous time step needs to be completed, a lock/unlock wouldn't work as even if the data is free for modification the prerequisite calculations may not have been done, this is where you would use a wait/signal.

Cramer
  • 1,785
  • 1
  • 12
  • 20
0

Cramer your answer gave me good hints but the answer on this page was exactly the explanation I needed.

Conditional Variable vs Semaphore

Community
  • 1
  • 1
TheIronChef
  • 81
  • 1
  • 4