what does it mean by thread serialization in c++?

前端 未结 3 1731
后悔当初
后悔当初 2021-02-20 01:13

I know about serializing objects and how they are saved to disk, but what does thread serialization actually mean? Could any one help me on this one and point me in the right di

相关标签:
3条回答
  • 2021-02-20 01:55

    Serialize actually means to publish in a serial form, as in one after another. So Thread Serialization means to make sure that a certain set of events occurs in a sequence not at the same time

    So a heap that has serialized thread access means that calls to the heap will occur in the order that they are made and will not actually occur at the same time. This is probably done by the means of a global lock.

    0 讨论(0)
  • 2021-02-20 02:11

    Without knowing the specific context in which you were told this, I guess that it refers to the development of a single threaded application that simulates a multi-threaded system by using a message queue.

    This brings me back to the old days when I played a CircleMUD derivative. The entire game was implemented in a single thread, but it had to deal with 50-100 players simultaneously. I was quite amazed at how they accomplished that.

    0 讨论(0)
  • 2021-02-20 02:17

    You are right that these are two different meanings of serialization. You are familiar with data serialization which is to convert a data structure into a stream of bytes in some canonical representation. In multi-threading the term serialization means mutual exclusion for thread or process synchronization which means only one thread may operate on a data structure at a time. C++11 provides for serialization between threads using a std::mutex

    #include <mutex>
    std::mutex file_io_mutex;
    
    {
        std::lock_guard<std::mutex> guard(file_io_mutex);
        std::out << "Only one thread at a time will execute this line." << std::endl;
    }
    

    This is an example of Resource Acquisition Is Initialization (RAII) where the resource is acquired and initialized at the same time, and released when it goes out of scope (execution reaches the close curly bracket). This is a common idiom, it ensures that the mutex is released even if the code throws an exception before reaching the end of the block.

    0 讨论(0)
提交回复
热议问题