What is syncblock?

4

I received a response that spoke about a syncblock . Why is it necessary?

From what I understand, what I researched is competition control. But why does every object need it if not everyone will have competition?

    
asked by anonymous 28.04.2017 / 16:34

2 answers

4

In fact its main function is competition control, but it is not the only one, somehow there would have to be that "block" in the object header.

It has the address of a synchronization object that will ensure control over access to the object. The object of synchronization is more complex, could not be in each object, then only a reference to this object is placed.

Some people think it should not be by default and only the objects that can actually be synchronized should be that overhead . But in practice almost every object can be synchronized, although in general they are not. It is a complication to have to choose this if almost everyone will have this cost.

Without it the synchronization would have to be done by the operating system which implies in exchange of execution context that is something very expensive in processing. So we opted to spend a little memory to have our own synchronization mechanism which is much faster.

They could have opted for some other way. Could have a global table of control. There would be an extra processing cost to find the object in the table, but it would save space. But either they found this solution better or they did not think of it sooner.

    
28.04.2017 / 16:44
2

Synchronization Guarantee

One function of syncblock is to do this concurrency control in a multitasking system. I do not know if it is in this sense that you have doubt, but I will set an example.

Suppose on your system you have a class that implements a Singleton . In this scenario there is then an instance of a class running on the system, and this instance is unique to any call that is made to the methods. If you have two thread running on the system and this means that the two can call the same method as Singleton at the same time. If you do not do any control, depending on the code that is executed in the method, your program may work incorrectly.

One way to ensure that these two calls will not have execution problems, you need to synchronize their execution, that is, you need to ensure that if the two thread calls the method at the same time, one will execute before other and so there will be no interference. To do this, we use that command block lock .NET.

For example, I'll introduce a code to implement a Singleton in a secure and synchronized way for a multitasking system:

public sealed class Session
{

    private static volatile Session instance;
    private static object sync = new Object();

    private Session() { }

    public static Session Instance
    {
        get
        {
            if (instance == null)
            {
                lock (sync)
                {
                    if (instance == null)
                    {
                        instance = new Session();
                    }
                }
            }
            return instance;
        }

    }

}

The lock block activates this control on the sync variable by allocating execution control ownership to that thread that called the Instance method. This way, when the second thread calls the same Instance method, the system will try to call lock to sync and the result will be a temporary block in the execution of that second thread . When the first thread exits the block lock , it will release the control in the variable sync thus allowing the unlocking of the second thread and the continued execution of it.

How the sample code was implemented will ensure that only one instance of the Session class is created. The second thread that runs, when passing lock will identify that the instance has already been created and will receive it. If there were no such control, there would be a chance that each of the thread would receive a new instance of the class Session and then we would not have a Singleton anymore.

What your program did was to create a sort of forced synchronization in that part of the code so that it would not be executed by two threads at the same time. The syncblock is then a reserved area in memory for that object so that it allows you to use this synchronization function at certain critical points of the code.

    
28.04.2017 / 19:52