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.
精彩评论