开发者

What does Thread Safety mean?

开发者 https://www.devze.com 2023-02-16 00:15 出处:网络
I\'ve been reading som开发者_StackOverflow社区e blogs on multi-threaded programming in ruby. What I noticed is that the author tends to use the word thread-safety. What does it mean? Why is it importa

I've been reading som开发者_StackOverflow社区e blogs on multi-threaded programming in ruby. What I noticed is that the author tends to use the word thread-safety. What does it mean? Why is it important to write thread-safety code?


If you have a resource (let's say a global List of Books for example) and you have two threads running which can modify this list. There are a lot of situations where the data of the list will get inconsistent.

  • (Thread A reads a Book ands displays its Data)
  • (Thread B deletes the same Book while the Data is used by Thread A)
  • (Thread A now wants to add some information to the Book)

So you have to make your code thread-safe so that at anytime only one single thread can have write access to the list of books.

Deadlocking mentioned by SpyrosP happens when Thread A blocks the List for writing and waits for Thread B to add data on the list. Because both threads will wait for each other to do something they can't do. That only happens if the thread-safety mechanism is not properly implemented.

0

精彩评论

暂无评论...
验证码 换一张
取 消