0xCODE
Published in

0xCODE

Concurrent processing systems and MUTEX

Mutual Exclusion (MUTEX) In Single Core Concurrent Systems

Concurrency Control

Race Conditions

The full components of a food delivery service.
This simplified diagram uses Dekker’s Algorithm as an example. If two threads simultaneously attempt to enter a critical section, only one process is allowed. This can be based on priority or schedule. In this example I used a value of 0 as being a higher priority so thread 2 is executed before thread 1.
  1. Read the integer value
  2. Increment the value by 1
  3. Write the value back to memory resulting in an output of 1
  1. Read the integer value
  2. Increment the value by 2
  3. Write back the value to memory resulting in the output of 3 (1 + 2)
An illustration of the above example.

Implementing MUTEX

Example Of MUTEX In Code

from threading import Thread, Lock
import threading
mutex = Lock()


def processData(data, thread_safe):
if thread_safe:
mutex.acquire()
try:
thread_id = threading.get_ident()
print('\nProcessing data:', data, "ThreadId:", thread_id)
finally:
if thread_safe:
mutex.release()


counter = 0
max_run = 100
thread_safe = True
while True:
some_data = counter
t = Thread(target=processData, args=(some_data, thread_safe))
t.start()
counter = counter + 1
if counter >= max_run:
break
Processing data: 0 ThreadId: 123145505792000Processing data: 1 ThreadId: 123145511047168Processing data: 2 ThreadId: 123145505792000Processing data: 3 ThreadId: 123145505792000Processing data: 4 ThreadId: 123145511047168Processing data: 5 ThreadId: 123145505792000Processing data: 6 ThreadId: 123145511047168Processing data: 7 ThreadId: 123145505792000Processing data: 8 ThreadId: 123145511047168Processing data: 9 ThreadId: 123145505792000Processing data: 10 ThreadId: 123145511047168Processing data: 11 ThreadId: 123145505792000Processing data: 12 ThreadId: 123145511047168Processing data: 13 ThreadId: 123145505792000Processing data: 14 ThreadId: 123145516302336Processing data: 15 ThreadId: 123145511047168Processing data: 16 ThreadId: 123145505792000Processing data: 17 ThreadId: 123145516302336Processing data: 18 ThreadId: 123145511047168Processing data: 19 ThreadId: 123145505792000Processing data: 20 ThreadId: 123145516302336Processing data: 21 ThreadId: 123145521557504Processing data: 22 ThreadId: 123145511047168Processing data: 23 ThreadId: 123145505792000Processing data: 24 ThreadId: 123145526812672Processing data: 25 ThreadId: 123145516302336Processing data: 26 ThreadId: 123145521557504Processing data: 27 ThreadId: 123145511047168Processing data: 28 ThreadId: 123145505792000Processing data: 29 ThreadId: 123145526812672Processing data: 30 ThreadId: 123145532067840Processing data: 31 ThreadId: 123145516302336Processing data: 32 ThreadId: 123145521557504Processing data: 33 ThreadId: 123145511047168Processing data: 34 ThreadId: 123145505792000Processing data: 35 ThreadId: 123145526812672Processing data: 36 ThreadId: 123145532067840Processing data: 37 ThreadId: 123145516302336Processing data: 38 ThreadId: 123145511047168Processing data: 39 ThreadId: 123145505792000Processing data: 40 ThreadId: 123145521557504Processing data: 41 ThreadId: 123145526812672Processing data: 42 ThreadId: 123145532067840Processing data: 43 ThreadId: 123145516302336Processing data: 44 ThreadId: 123145511047168Processing data: 45 ThreadId: 123145537323008Processing data: 46 ThreadId: 123145505792000Processing data: 47 ThreadId: 123145521557504Processing data: 48 ThreadId: 123145542578176Processing data: 49 ThreadId: 123145526812672Processing data: 50 ThreadId: 123145532067840Processing data: 51 ThreadId: 123145547833344Processing data: 52 ThreadId: 123145516302336Processing data: 53 ThreadId: 123145511047168Processing data: 54 ThreadId: 123145537323008Processing data: 55 ThreadId: 123145505792000Processing data: 56 ThreadId: 123145553088512Processing data: 57 ThreadId: 123145521557504Processing data: 58 ThreadId: 123145542578176Processing data: 59 ThreadId: 123145526812672Processing data: 60 ThreadId: 123145532067840Processing data: 61 ThreadId: 123145516302336Processing data: 62 ThreadId: 123145511047168Processing data: 63 ThreadId: 123145537323008Processing data: 64 ThreadId: 123145505792000Processing data: 65 ThreadId: 123145547833344Processing data: 66 ThreadId: 123145521557504Processing data: 67 ThreadId: 123145526812672Processing data: 68 ThreadId: 123145532067840Processing data: 69 ThreadId: 123145511047168Processing data: 70 ThreadId: 123145516302336Processing data: 71 ThreadId: 123145505792000Processing data: 72 ThreadId: 123145537323008Processing data: 73 ThreadId: 123145521557504Processing data: 74 ThreadId: 123145526812672Processing data: 75 ThreadId: 123145511047168Processing data: 76 ThreadId: 123145516302336Processing data: 77 ThreadId: 123145505792000Processing data: 78 ThreadId: 123145521557504Processing data: 79 ThreadId: 123145526812672Processing data: 80 ThreadId: 123145511047168Processing data: 81 ThreadId: 123145516302336Processing data: 82 ThreadId: 123145505792000Processing data: 83 ThreadId: 123145521557504Processing data: 84 ThreadId: 123145511047168Processing data: 85 ThreadId: 123145516302336Processing data: 86 ThreadId: 123145526812672Processing data: 87 ThreadId: 123145505792000Processing data: 88 ThreadId: 123145521557504Processing data: 89 ThreadId: 123145532067840Processing data: 90 ThreadId: 123145511047168Processing data: 91 ThreadId: 123145516302336Processing data: 92 ThreadId: 123145537323008Processing data: 93 ThreadId: 123145526812672Processing data: 94 ThreadId: 123145505792000Processing data: 95 ThreadId: 123145542578176Processing data: 96 ThreadId: 123145521557504Processing data: 97 ThreadId: 123145532067840Processing data: 98 ThreadId: 123145547833344Processing data: 99 ThreadId: 123145511047168
Processing data: 0 ThreadId: 123145463066624Processing data: 1 ThreadId: 123145468321792Processing data: 2 ThreadId: 123145463066624Processing data: 3 ThreadId: 123145463066624Processing data: 4 ThreadId: 123145468321792Processing data: 5 ThreadId: 123145463066624Processing data: 6 ThreadId: 123145463066624Processing data: 7 ThreadId: 123145468321792Processing data: 8 ThreadId: 123145463066624Processing data: 9 ThreadId: 123145468321792Processing data: 10 ThreadId: 123145463066624Processing data: 11 ThreadId: 123145468321792Processing data:Processing data: 12 ThreadId: 12314546306662413 ThreadId: 123145468321792Processing data:Processing data:Processing data:Processing data: 16 ThreadId: 123145463066624Processing data:Processing data: 14 ThreadId: 123145473576960Processing data:Processing data:Processing data: 18 ThreadId: 123145484087296Processing data:Processing data: 19 ThreadId: 12314548934246422 ThreadId: 123145463066624Processing data: 15 ThreadId: 12314547883212817 ThreadId: 12314546832179224 ThreadId: 123145510363136Processing data:Processing data:Processing data: 20 ThreadId: 12314549459763227 ThreadId: 123145520873472Processing data:Processing data:Processing data:Processing data:Processing data: 26 ThreadId: 123145515618304Processing data:Processing data:Processing data:Processing data: 34 ThreadId: 123145541894144Processing data: 25 ThreadId: 12314547357696036 ThreadId: 123145468321792Processing data:Processing data: 31 ThreadId: 123145536638976Processing data:Processing data:Processing data: 33 ThreadId: 123145463066624Processing data:Processing data: 29 ThreadId: 123145484087296Processing data:Processing data:Processing data: 30 ThreadId: 12314553138380837 ThreadId: 12314551036313645 ThreadId: 123145573425152Processing data:Processing data: 23 ThreadId: 12314550510796847 ThreadId: 123145583935488Processing data:Processing data: 40 ThreadId: 123145557659648Processing data:Processing data:Processing data: 42 ThreadId: 123145520873472Processing data:Processing data: 43 ThreadId: 12314556291481653 ThreadId: 123145541894144Processing data:Processing data:Processing data:Processing data: 38 ThreadId: 12314554714931257 ThreadId: 123145615466496Processing data:Processing data: 39 ThreadId: 12314555240448048 ThreadId: 12314551561830460 ThreadId: 123145625976832Processing data:Processing data: 28 ThreadId: 12314552612864062 ThreadId: 123145636487168Processing data: 63 ThreadId: 12314546306662451 ThreadId: 123145599700992Processing data: 35 ThreadId: 12314547883212865 ThreadId: 123145646997504Processing data: 84 ThreadId: 123145704804352Processing data:Processing data:Processing data:Processing data: 55 ThreadId: 123145473576960Processing data: 56 ThreadId: 123145468321792Processing data:Processing data:Processing data: 58 ThreadId: 12314562072166432 ThreadId: 123145489342464Processing data: 59 ThreadId: 123145536638976Processing data: 95 ThreadId: 123145741590528Processing data: 49 ThreadId: 12314558919065677 ThreadId: 123145678528512Processing data: 41 ThreadId: 123145494597632Processing data: 50 ThreadId: 12314559444582480 ThreadId: 12314568903884821 ThreadId: 123145499852800Processing data: 66 ThreadId: 123145484087296Processing data: 54 ThreadId: 12314561021132844 ThreadId: 12314556816998487 ThreadId: 12314554189414469 ThreadId: 123145662763008Processing data: 90 ThreadId: 12314572582502472 ThreadId: 123145573425152Processing data: 73 ThreadId: 12314566801817674 ThreadId: 123145673273344Processing data:Processing data:Processing data:Processing data:Processing data:Processing data: 52 ThreadId: 123145604956160Processing data: 85 ThreadId: 12314571005952068 ThreadId: 123145657507840Processing data: 46 ThreadId: 123145578680320Processing data: 93 ThreadId: 123145615466496Processing data: 61 ThreadId: 12314563123200096 ThreadId: 12314555240448098 ThreadId: 12314562597683299 ThreadId: 123145746845696Processing data: 82 ThreadId: 12314569954918467 ThreadId: 123145652252672Processing data: 70 ThreadId: 123145531383808Processing data: 92 ThreadId: 123145547149312Processing data: 97 ThreadId: 12314551561830478 ThreadId: 12314568378368079 ThreadId: 12314555765964881 ThreadId: 12314569429401686 ThreadId: 12314556291481688 ThreadId: 12314571531468871 ThreadId: 12314551036313675 ThreadId: 12314550510796864 ThreadId: 12314564174233689 ThreadId: 12314572056985691 ThreadId: 12314573108019276 ThreadId: 12314558393548894 ThreadId: 12314573633536083 ThreadId: 123145520873472
Processing data: 38 ThreadId: 123145511047168
96 ThreadId: 12314555240448098 ThreadId: 12314562597683299 ThreadId: 123145746845696

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vincent Tabora

Editor HD-PRO, DevOps Trusterras (Cybersecurity, Blockchain, Software Development, Engineering, Photography, Technology)