Introduction To Multithreading : Apps Powered by Multicore processors on Window Phone 7

c7875ff7f880fe46f6b3067ecc30a41a-server-clip-art

The advantage of threading is the ability to create applications that use more than one thread of execution. For example, a process can have a user interface thread that manages interactions with the user and worker threads that perform other tasks while the user interface thread waits for user input.

This tutorial demonstrates various thread activities:

  • Creating and executing a thread
  • Synchronization of threads
  • Interaction between threads
  • Using a thread pool
  • Using a mutex object to protect a shared resource
  • See Threading Sample to download and build the sample files discussed in this tutorial.

    Sample Files

     

    Example 1: Creating, starting, and interacting between threads

    This example demonstrates how to create and start a thread, and shows the interaction between two threads running simultaneously within the same process. Note that you don’t have to stop or free the thread. This is done automatically by the .NET Framework common language runtime.

    The program begins by creating an object of type Alpha (oAlpha) and a thread (oThread) that references the Beta method of the Alpha class. The thread is then started. TheIsAlive property of the thread allows the program to wait until the thread is initialized (created, allocated, and so on). The main thread is accessed through Thread, and theSleep method tells the thread to give up its time slice and stop executing for a certain amount of milliseconds. The oThread is then stopped and joined. Joining a thread makes the main thread wait for it to die or for a specified time to expire (for more details, see Thread.Join Method). Finally, the program attempts to restart oThread, but fails because a thread cannot be restarted after it is stopped (aborted). For information on temporary cessation of execution, see Suspending Thread Execution.

    Example Output

    Example 2: Synchronizing two threads: a producer and a consumer

    The following example shows how synchronization can be accomplished using the C# lock keyword and the Pulse method of the Monitor object. The Pulse method notifies a thread in the waiting queue of a change in the object’s state (for more details on pulses, see the Monitor.Pulse Method).

    The example creates a Cell object that has two methods: ReadFromCell and WriteToCell. Two other objects are created from classes CellProd and CellCons; these objects both have a method ThreadRun whose job is to call ReadFromCell and WriteToCell. Synchronization is accomplished by waiting for “pulses” from the Monitor object, which come in order. That is, first an item is produced (the consumer at this point is waiting for a pulse), then a pulse occurs, then the consumer consumes the production (while the producer is waiting for a pulse), and so on.

    Example Output

     

    Example 3: Using a thread pool

    The following example shows how to use a thread pool. It first creates a ManualResetEvent object, which enables the program to know when the thread pool has finished running all of the work items. Next, it attempts to add one thread to the thread pool. If this succeeds, it adds the rest (four in this example). The thread pool will then put work items into available threads. The WaitOne method on eventX is called, which causes the rest of the program to wait until the event is triggered (with the eventX.Set method). Finally, the program prints out the load (the thread that actually executes a particular work item) on the threads.