Example Code

Deadlock with Semaphores

Code and Documents

Attachment

I had come across the issue of deadlock in another language and wondered how to force this situation in LabVIEW.

A way to overcome race conditions in local variables is to use semaphores.  This can be used to put a mutex around a block of code to prevent another block of code from executing.  However there can be an instance when two semaphores can block each other out by not releasing in the correct order.

In this code I have an increment loop which cannot acquire semaphore 2 until the decrement loop releases it.  However, the decrement loop cannot release semaphore 2 until the increment loop releases semaphore 1.  This creates a deadlock situation where the threads are waiting on each other and the program hangs.

Deadlock with Semaphores.png

Enjoy!


Rob

Robert Ward
Applications Engineer, NI

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
Rob_Ward
NI Employee (retired)
on

The VI snipped has changed the local variables for value change property nodes!

Robert Ward
Applications Engineer, NI
Eden_S
NI Employee (retired)
on

Nice example Rob_Ward!

Eden S
Applications Engineer
National Instruments UK & Ireland
Contributors