views:

356

answers:

5

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 did'nt quite understand why all the extra code is necessary

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

Controls.CheckForIllegalCrossThread..blah =true

+3  A: 

Wikipedia has an article on Thread Safety.

This definitions page (you have to skip an ad - sorry) defines it thus:

In computer programming, thread-safe describes a program portion or routine that can be called from multiple programming threads without unwanted interaction between the threads.

A thread is an execution path of a program. A single threaded program will only have one thread and so this problem doesn't arise. Virtually all GUI programs have multiple execution path and hence threads - one for processing the display of the GUI and handing user input, others for actually performing the operations of the program. This is so that the UI is still responsive while the program is working.

ChrisF
+1  A: 

You are clearly working in a WinForms environment. WinForms controls exhibit thread affinity, which means that the thread in which they are created is the only thread that can be used to access and update them. That is why you will find examples on MSDN and elsewhere demonstrating how to marshall the call back onto the main thread.

Normal WinForms practice is to have a single thread that is dedicated to all your UI work.

David M
+2  A: 

In the simplest of terms threadsafe means that it is safe to be accessed from multiple threads. When you are using multiple threads in a program and they are each attempting to access a common data structure or location in memory several bad things can happen. So, you add some extra code to prevent those bad things. For example, if two people were writing the same document at the same time, the second person to save will overwrite the work of the first person. To make it thread safe then, you have to force person 1 to wait for person 2 to complete their task before allowing person 1 to edit the document.

Vincent Ramdhanie
+4  A: 

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.

Definitely worth a read!

Gregory Pakosz
A: 

A module is thread-safe if it guarantees it can maintain its invariants in the face of multi-threaded and concurrence use.

Here, a module can be a data-structure, class, object, method/procedure or function. Basically scoped piece of code and related data.

The guarantee can potentially be limited to certain environments such as a specific CPU architecture, but must hold for those environments. If there is no explicit delimitation of environments, then it is usually taken to imply that it holds for all environments that the code can be compiled and executed.

Thread-unsafe modules may function correctly under mutli-threaded and concurrent use, but this is often more down to luck and coincidence, than careful design. Even if some module does not break for you under, it may break when moved to other environments.

Multi-threading bugs are often hard to debug. Some of them only happen occasionally, while others manifest aggressively - this too, can be environment specific. They can manifest as subtly wrong results, or deadlocks. They can mess up data-structures in unpredictable ways, and cause other seemingly impossible bugs to appear in other remote parts of the code. It can be very application specific, so it is hard to give a general description.

Christian Vest Hansen