1

I can't understand the purpose of Synchronization context abstraction in .net.

Is it possible to say that Synchronization context is an abstraction over group of threads (one or more) which I will call by analogy of the term "object model" or "thread model"? I mean that Sync. context hides threads from us which are capable of doing some work — they can do their specific tasks (update UI or some other complex work), but we don’t need to know how, we just have some very abstract high level interface like Post to ask them do something for us. And all these threads are specifically dedicated to do this sole job.

Does it make sense? Can I use the terms "threading model" and synchronization context interchangeably?

Thanks in advance.

SparkFountain
  • 2,110
  • 15
  • 35
Sharov
  • 458
  • 8
  • 38
  • 3
    Have a look at this article https://learn.microsoft.com/en-us/archive/msdn-magazine/2011/february/msdn-magazine-parallel-computing-it-s-all-about-the-synchronizationcontext – Pavel Anikhouski Feb 03 '20 at 11:16
  • And then https://blog.stephencleary.com/2012/02/async-and-await.html and the links from there. – GSerg Feb 03 '20 at 11:17
  • @PavelAnikhouski: Thank you, so in other terms I can think of sync. context like a threading model of some kind (for some specific work). Is it correct? – Sharov Feb 03 '20 at 11:22
  • @Sharov No. Async is not about threads, even though an implementation of async may or may not use threads under the hood. See https://stackoverflow.com/q/17661428/11683, https://stackoverflow.com/q/27265818/11683 and https://stackoverflow.com/q/37419572/11683. – GSerg Feb 03 '20 at 11:29
  • It probably helps if you try to understand it in its own name. It's a context in which you can synchronize work from different threads. For UI-apps, the context is usually the main thread, so that all synchronized work happens sequentially on one thread. – Lasse V. Karlsen Feb 03 '20 at 11:29
  • 1
    @Sharov please, read the _The Concept of SynchronizationContext_ section in article above, I don't think that synch context is a threading model at any aspect – Pavel Anikhouski Feb 03 '20 at 11:29
  • 1
    The `SynchronizationContext` is a kind of scheduler. You schedule asynchronous work by calling its method `Post`. There are multiple `SynchronizationContext` implementations, and each one may handle the scheduled work differently. You may find it easier to understand it by comparing it with another kind of scheduler, the `TaskScheduler`: [What is the conceptual difference between SynchronizationContext and TaskScheduler](https://stackoverflow.com/questions/9580061/what-is-the-conceptual-difference-between-synchronizationcontext-and-taskschedul). – Theodor Zoulias Feb 03 '20 at 23:27
  • @PavelAnikhouski: Thanks for link on msdn. I clarify my understanding of sync. context a little bit. And yes, I'm still insist that it abstracts away threading model of some specific domain, like ASP.net in link above. As word context suggests it holds some specific (to domain) information needed to handle some queued work. And yes, it is in close relation with callbacks execution, where we need to execute callbacks in some specific domain(context) like UI. Probably when we dealing with callbacks it is better to look at sync. context as scheduler. – Sharov Feb 04 '20 at 12:55

0 Answers0