Let’s say I have a thread pool that has 5 child threads. And they are calling a function called “functionA()” How do I make the function to be thread safe?
Also if those 5 threads are called at the same time then are they executed concurrently?
or do they wait until a thread that currently works in the function to be finished ?
Thanks in advance..
A function is already thread safe if it doesn’t modify non-local memory and it doesn’t call any function that does. In this (trivial) case, you don’t have to do anything.
You really want to think about protecting data, not functions. For example, let’s say that the function modifies non-local data structure X. Provide a mutex to protect X and lock it before each access and unlock it after. You may have more than one function that accesses X (e.g. insertX(), deleteX(), …). As long as you protect the data you’ll be OK.