Later,įrom Framework 4.0, there is also the SpinLock In this section, we’ll start with the lockĪnd semaphores (for nonexclusive locking). Lock can span applications in different processes on the computer. Of the two, the lock construct is faster and moreĬonvenient. Thread’s state may change in between testing ThreadStateĮxclusive locking is used to ensure that only one threadĬan enter particular sections of code at a time. Useful for diagnostic purposes, but unsuitable for synchronization, because a Public static ThreadState SimpleThreadState (ThreadState ts) To one of the four most useful values: Unstarted, Running, WaitSleepJoin, and Stopped: Most values, however, are redundant, unused, or deprecated. This returns a flags enum of type ThreadState, which combines three “layers” of data in aīitwise fashion. You can query a thread's execution status via its ThreadState property. NETįramework provides special methods and classes to assist - these are covered Spinning very briefly can be effective when youĮxpect a condition to be satisfied soon (perhaps within a few microseconds)īecause it avoids the overhead and latency of a context switch. Problems can arise, though, due toĬoncurrency issues on the proceed flag. Sometimes a hybrid between blocking and spinning is used:Īlthough inelegant, this is (in general) far moreĮfficient than outright spinning. Important calculation, and so gets allocated resources accordingly! In general, this is very wasteful on processor time: asįar as the CLR and operating system are concerned, the thread is performing an However, there is a simpler alternative: a thread can await aĬondition by spinning in a polling loop. Sometimes a thread must pause until a certain condition isĬonstructs achieve this efficiently by blocking until a condition is by being interrupted via Thread.InterruptĪ thread is not deemed blocked if its execution is paused.by the operation timing out (if a timeout is specified). by the blocking condition being satisfied.Unblocking happens in one of four ways (the computer's When a thread blocks or unblocks, the operating system Its state and then acting upon that information, this code is useful only in (Given that a thread’s state may change in between testing Interactive development in a standalone executable! Test for a thread being blocked via its ThreadState property:īool blocked = (someThread.ThreadState & ThreadState.WaitSleepJoin) != 0 A blocked thread immediately yields its processor time slice, and from then onĬonsumes no processor time until its blocking condition is satisfied. Waiting for another to end via Join or EndInvoke. Paused for some reason, such as when Sleeping or Let’sĪ thread is deemed blocked when its execution is The CLR and C# provide the following nonblockingĬonstructs: Thread.MemoryBarrier, Thread.VolatileRead, Thread.VolatileWrite, theīlocking is essential to all but the last category. Nonblocking synchronization constructs These protect access to a common field by calling upon Framework 4.0 introduces the CountdownEvent and Barrier classes. Two commonly used signaling devices: event Notification from another, avoiding the need for inefficient polling. Signaling constructs These allow a thread to pause until receiving a The nonexclusive locking constructsĪre Semaphore, SemaphoreSlim, and the reader/writer locks. The standard exclusive locking constructs are lock ( Monitor.Enter/ Monitor.Exit), Mutex,Īnd SpinLock. Exclusive lockingĬonstructs are most common - these allow just one thread in at a time, and allowĬompeting threads to access common data without interfering with each other. Locking constructs These limit the number of threads that can perform someĪctivity or execute a section of code at a time. Synchronization constructs can be divided into fourĬategories: Simple blocking methods These wait for another thread to finish or for a periodĪnd Task.Wait are simple blocking methods. Is particularly important when threads access the same data it’s surprisingly Threads, allowing them to communicate via common fields.Ĭoordinating the actions of threads for a predictable outcome. Local variables are private to a thread and how references can be shared among So far, we’ve described how to start a task on a thread,Ĭonfigure a thread, and pass data in both directions. Part 2: Basic Synchronization Synchronization Essentials
0 Comments
Leave a Reply. |