What is the syncblock?

Asked

Viewed 83 times

5

I received a reply about a syncblock. Why is it necessary?

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

2 answers

4


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

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

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

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

They could have opted for some other way. It could have a global control table. 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 didn’t think of it before.

Syncblock

3

Synchronization guarantee

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

Suppose in 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 the Singleton at the same time. If you do not do any controls, depending on the code that runs in the method, your program may work incorrectly.

One of the ways to ensure that these two calls will not run into problems is to synchronize their execution, that is, you need to ensure that if the two thread call the method at the same time, one will run before the other and so there will be no interference. For this, we use that command block lock of . NET.

For example, I will present a code to implement a Singleton safely and synchronized to 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 block of lock activates this control in the variable sync allocating possession of the enforcement control to that thread who called the method Instance. That way, when the second thread call the same method Instance, the system will try to call the lock to the sync and the result will be a temporary block in the execution of that second thread. When the first thread exit the block lock, it will release the control in the variable sync thus allowing the release of the second thread and the continued execution of it.

The way the sample code has been implemented will ensure that only one instance of the class Session be created. The second thread that rotate, when passing through the lock will identify that the instance has already been created and will receive it. If there was no such control, there would be a chance for each of the thread receive a new class instance Session and then we wouldn’t have another Singleton.

What your program has done is to create a kind of forced synchronization on that part of the code so that it is not executed by two threads at the same time. The syncblock then it is an area reserved in memory for that object so that it allows to use this synchronization function at certain critical points of the code.

  • Very good explanation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.