Race Condition (Patreon)
Downloads
Content
Thank you everyone for subscribing and supporting my desktop pets.
I'll be busy with my school works/exams for a while and will continue drawing without creating other apps or tools.
I know most people subscribe for the desktop pets rather than my other artworks. I'll keep thinking about what kind of art people will like.
--Race Condition--
In the real world, once the first sperm fertilizes the egg, the egg undergoes a series of reactions that effectively block the surface and prevent other sperm from entering. Only in rare cases does polyspermy occur, in which multiple sperm enter the same egg.
However, in the computer world, simply relying on a very fast locking reaction is meaningless to the CPU and a multithreaded program. Multiple sperm can still attempt to access the same egg at nearly the same time.
Suppose there are 20 sperm, and we define fertilization as:
“Check whether the egg exists → delete the old egg → create the fertilized egg.”
When a sperm is about to enter the egg, it must first check:
“Is the egg still there?”
The problem is that the “check” and the subsequent operation are not naturally indivisible.
In other words, the computer does not guarantee that a sperm will continue executing the next step immediately after checking the state of the egg.
For example:
Sperm A: Check that the egg exists -> Gets interrupted / switched out
Sperm B: Check that the egg exists-> Gets interrupted / switched out
Sperm A: Accesses the egg and changes its state
Sperm B: Continues execution
At this point, the state that Sperm B observed when it performed its check may no longer be the same when it actually performs the next operation.
The CPU and operating system scheduler do not guarantee that Sperm A will always execute before or after Sperm B, nor do they guarantee that Sperm B will immediately run after Sperm A finishes a particular step.
As a result, something that should normally allow only one sperm to succeed may cause multiple sperm to believe that they have successfully fertilized the egg.
This situation, where the result of a program depends on which thread happens to reach a particular step first, is called a Race Condition.
Therefore, unlike a biological egg, a computer cannot simply rely on changing its state quickly to prevent other sperm from accessing it. If a shared piece of data can only be modified by one thread at a time, the programmer must use a synchronization mechanism, such as a Mutex (mutual exclusion lock), to control access to the egg.
With a mutex, the first sperm to acquire the lock can complete the entire “check → access → modify” sequence while holding the lock. Other sperm must wait, and after acquiring the lock, they must check again whether the egg still exists.
Actual program demonstration as follows:
謝謝大家訂閱支持我製作的桌寵
接下來我會忙一陣子大學課業 會繼續單純畫圖不打算製作其他應用或工具
我知道大部分人訂閱是為了桌寵而非其他自己的插畫,我會思考怎樣的畫才是有人喜歡的。
--Race Condition--
現實世界中,第一顆精子進入卵子結合後,卵子會進行一系列反應上鎖表層,避免其餘精子鑽入,僅有罕見情況會發生多精子鑽入卵子的現象。
然而在電腦世界中,這個僅靠快速完成上鎖的機制,對 CPU 與多執行緒而言是沒有意義的;多個精子仍然可能同時存取同一顆卵子。
假設有 20 顆精子,並將受精定義為:
「確認是否存在卵子 → 刪除舊卵子 → 生成受精的卵子」
當其中一顆精子準備進入卵子時,它必須先確認:
「卵子還在嗎?」
問題在於,這個「確認」和「取得」並不是天然不可分割的。
或者說,電腦世界不保證一個精子確認卵子狀態後,一定會立刻繼續完成下一步流程。
可能發生:
精子 A:確認卵子還在 ->被切換
精子 B:確認卵子還在->被切換
精子 A:取得卵子並改變狀態
精子 B:繼續執行
此時精子 B 在確認卵子時所看到的狀態,和它真正執行後續操作時的狀態可能已經不同。
CPU 與作業系統的排程並不保證精子 A 一定會在精子 B 後執行,也不保證精子 A 執行完某個步驟後就會立刻輪到精子 B。
因此,原本應該只有一顆精子成功的事情,在電腦世界中可能變成多個精子都認為自己成功受精。
這種*程式執行結果取決於不同執行緒「誰先跑到哪一步」*的情況,就稱為 Race Condition(競爭條件)。
因此,電腦世界不能像現實生物的卵子一樣,只靠快速改變狀態來避免其他精子進入。如果一份共享資料同一時間只能允許一個執行緒修改,就必須由程式設計者建立同步機制,例如 Mutex(互斥鎖),讓精子在存取卵子時依序進入。
如此一來,第一顆取得鎖的精子可以在鎖定期間完成「檢查 → 取得 → 修改」的整個流程;後續精子必須等待,等取得鎖後再重新確認卵子是否仍然存在。
實際程式演示如下:
NO LOCK:
MUTEX LOCK: