Thread safety

Thread safety is a computer programming concept applicable in the context of multi-threaded programs. A piece of code is thread-safe if it only manipulates shared data structures in a manner that guarantees safe execution by multiple threads at the same time.

->3 main thread synchronization techniques in c#
->lock, mutex, semaphore

-> When two or more threads need to access a shared resource at the same time, the system needs a synchronization mechanism to ensure that only one thread at a time uses the resource. Mutex is a synchronization primitive that grants exclusive access to the shared resource to only one thread. If a thread acquires a mutex, the second thread which wants to acquire that mutex is suspended until the first thread releases the mutex.

-> We can use WaitHandle.WaitOne method to request ownership of a mutex.

Leave a Reply

Your email address will not be published. Required fields are marked *