
Burhan B.
asked 08/09/22How do I modify this c code so there are no race conditions and speed up the process by making a local variable
1 Expert Answer
Julie B. answered 10/04/24
PhD in Computer Science with 25+ Years Computer Programming / Coding
In regards to your race condition, find in your code where your threads are using the same variable(resource) at the same time. You need to stop this concurrent resource access from occurring with some sort of lock or mutex. (A mutex is a lock but can be system wide). There are also semaphores which allow a certain number of threads to utilize a pool of "like" resources. I.e. think of a semaphore as keeping a record of how many units of a particular resource are available, coupled with operations to adjust that record safely (to avoid race conditions), as units are acquired or become free, and, if necessary, wait until a unit of the resource becomes available. While semaphores are useful in preventing race conditions, they can not guarantee their absence. Semaphores allowing an arbitrary resource count are called counting semaphores, while semaphores restricted to the values 0 and 1 (i.e. locked/unlocked, unavailable/available) are called binary semaphores and are used to implement locks.
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Donald W.
You didn't provide any code.08/09/22