Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

16.1. Synchronizing Code

The key to writing thread-safe code is to remember that no thread is ever safe reading or writing a particular chunk of memory that might simultaneously get written to by another thread. If the memory that is being read or written to is also simultaneously changing from underneath the thread, the behavior of the application is undefined. Anything could happen. So the key then is to ensure that when you are writing to a particular block of memory or variable that no other thread will be able to read from it until you are finished.

The most common way to prevent another thread from accessing memory that you're writing to while you're writing it is the use of a mutually exclusive lock, or mutex.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint