Passing In Data To A Thread

Passing In Data To A Thread

 In this lecture, you will learn how to safely pass initialization data to a thread. You will learn about the ParameterizedThreadStart delegate, and how to use a lambda expression to initialize a thread. 

 Captured variables in lambda expressions are shared between the new thread and the main program thread, and so this opens us up to a possible race condition. 

 I will show you a short program that uses a lambda expression and introduces a race condition. Then I'll show you a cool trick, where I only change 2 lines of code, to make the race condition disappear. 



Write Multithreaded C# Code

Buy nowLearn more

Introduction

  • Course Introduction
  • Introduction To Threading
  • Windows, OS/X or Linux?

Working With Threads

  • Section Introduction
  • How To Start A Thread
  • First Problem: Race Conditions
  • Passing In Data To A Thread
  • Second Problem: Waiting On A Thread
  • Joining And Suspending Threads
  • Interrupting And Aborting Threads
  • Section Recap
  • Quiz 1: Working with threads

Introducing Thread Locking

  • Section Introduction
  • When Should You Lock Threads?
  • The Lock Statement
  • Dealing With Deadlocks
  • Assignment: Have Dinner With The Philosophers
  • Using The Interlocked Class
  • Section Recap
  • Quiz 2: Thread Locking

How To Synchronize Threads

  • Section Introduction
  • Thread Synchronization With AutoResetEvents
  • How To Build A Producer/Consumer Queue
  • The ManualResetEvent Class
  • The CountdownEvent Class
  • Thread Rendezvous
  • Section Recap
  • Quiz 3: Thread Synchronization

Final Words

  • Course Recap