What does threadsafe mean?

Vivek Bernard picture Vivek Bernard · Jan 9, 2010 · Viewed 78.2k times · Source

Recently I tried to Access a textbox from a thread (other than the UI thread) and an exception was thrown. It said something about the "code not being thread safe" and so I ended up writing a delegate (sample from MSDN helped) and calling it instead.

But even so I didn't quite understand why all the extra code was necessary.

Update: Will I run into any serious problems if I check

Controls.CheckForIllegalCrossThread..blah =true

Answer

Gregory Pakosz picture Gregory Pakosz · Jan 9, 2010

Eric Lippert has a nice blog post entitled What is this thing you call "thread safe"? about the definition of thread safety as found of Wikipedia.

3 important things extracted from the links :

“A piece of code is thread-safe if it functions correctly during simultaneous execution by multiple threads.”

“In particular, it must satisfy the need for multiple threads to access the same shared data, …”

“…and the need for a shared piece of data to be accessed by only one thread at any given time.”

Definitely worth a read!