What, exactly, is a race condition?

A race condition is a problem that can happen if a DOM is allowed to be accessed by web workers. A race condition is a condition where two threads race, or compete, to read/modify a single data source. This is dangerous because when both threads try to modify the data at same time, and it is unknown which one will modify the data first. Consider the following example.

Suppose two threads are working on the same variable in memory:

// thread 1 - pseudo program code
if variable == 5:
asyncOperationWhichTakes200MS()
// just here thread 2 modifies variable to 10
res = variable * 5
// res is now 50 instead of 25
// unpredicatable behavior ahead

Race conditions can be avoided by using semaphores, which is nothing but locking a shared data resource, until one thread is done with it and releases it back.

Just a fun fact: If you ever use sudo apt-get update on Ubuntu or any Linux distro supporting apt-get as the package manager, and try to run another apt-get update command in another Terminal, you'll get this error:

E: Unable to lock the administration directory (/var/lib/dpkg/) is another process using it?

Linux locks the directory to avoid a possible race condition in which two commands overwrite each other's results.

Most languages have just a single thread that interacts with and updates the UI, and other threads can only post messages to the main thread to update the UI.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.252.227