Thread safety is good, but sometimes we want all the resources and the members of those resources to be modified only by a certain thread. This is called thread affinity. Learn how this impacts other threads and how the other threads modify such resources.
- [Instructor] So what is thread affinity?…If you've worked on a WPF or Winforms application,…any attempt to access the UI element from any other…thread may lead to an exception or could even render the…state of the control invalid.…So thread affinity means that the thread, in this case the…UI thread that instantiates an object is the only thread…that can access its members.…So for example,…dependency object in WPF has thread affinity.…
So is thread affinity good or bad?…The answer is, it depends.…One of the advantages of having thread affinity is that…we may not need to use a lock to access…a UI object in WPF.…For that matter, if any thread has thread affinity we should…be able to access the objects inside that thread…simply because no other thread will…be able to access those objects,…so we don't really need to have a lock around them.…
However, it could also be disadvantageous…in certain situations.…So for example, if I want to make a call to a member on…object X created by thread X…from another thread Y,…the only way to do that would be marshaling the request…
Released
4/2/2018- Thread safety and affinity
- Signaling
- Task Parallel Library (TPL) basics
- PLINQ introduction
- Task-based Asynchronous Pattern (TAP)
Share this video
Embed this video
Video: Thread affinity