Don’t abuse critical sections

Critical section is a code block that cannot be executed by more than one process/thread at a time, which is why critical sections are expensive and can lead to a performance “bottle neck” at a specific flow.

Critical sections must be as short, simpl…


This content originally appeared on DEV Community and was authored by Or Yaacov

Critical section is a code block that cannot be executed by more than one process/thread at a time, which is why critical sections are expensive and can lead to a performance "bottle neck" at a specific flow.

Critical sections must be as short, simple (if possible), and efficient as possible.

For example, let's examine the following code block, which generates a new UUID for a shared object:

with self._mutex:
    # start of lock 
    try:
        self._action.id = str(uuid.uuid4())                      
        self._logger.trace("action generated, action_id={action_id}".format(action_id=self._action.id))
    except BaseException as err:
        self._logger.error(err,"failed to generate uuid")
    # end of lock

I did three common mistakes here:

  1. try & catch, ok, I admit it, probably not a common mistake since no one will put a try & catch that could be outside of a critical section inside of it without a good reason
  2. logging inside a critical section. Usually, logging is an expensive IO operation which could have be done outside
  3. generating the UUID inside the critical section

Here is the another code block which does the exact same thing, just without the three mistakes:

try:
    action_id = str(uuid.uuid4())
    with self._mutex:
        # start of lock
        self._action.id = action_id                  
        # end of lock

    self._logger.trace("action generated, action_id={action_id}".format(id=action_id))  
except BaseException as err:
    self._logger.error(err,"failed to generate uuid")

When you are using lock remember that is purpose is to avoid more than one thread access shared resources at the same time


This content originally appeared on DEV Community and was authored by Or Yaacov


Print Share Comment Cite Upload Translate Updates
APA

Or Yaacov | Sciencx (2022-05-07T17:30:03+00:00) Don’t abuse critical sections. Retrieved from https://www.scien.cx/2022/05/07/dont-abuse-critical-sections/

MLA
" » Don’t abuse critical sections." Or Yaacov | Sciencx - Saturday May 7, 2022, https://www.scien.cx/2022/05/07/dont-abuse-critical-sections/
HARVARD
Or Yaacov | Sciencx Saturday May 7, 2022 » Don’t abuse critical sections., viewed ,<https://www.scien.cx/2022/05/07/dont-abuse-critical-sections/>
VANCOUVER
Or Yaacov | Sciencx - » Don’t abuse critical sections. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/05/07/dont-abuse-critical-sections/
CHICAGO
" » Don’t abuse critical sections." Or Yaacov | Sciencx - Accessed . https://www.scien.cx/2022/05/07/dont-abuse-critical-sections/
IEEE
" » Don’t abuse critical sections." Or Yaacov | Sciencx [Online]. Available: https://www.scien.cx/2022/05/07/dont-abuse-critical-sections/. [Accessed: ]
rf:citation
» Don’t abuse critical sections | Or Yaacov | Sciencx | https://www.scien.cx/2022/05/07/dont-abuse-critical-sections/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.