Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 direction please?

like image 264
pokche Avatar asked Feb 01 '13 20:02

pokche


3 Answers

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.

like image 176
amdn Avatar answered Sep 21 '22 08:09

amdn


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.

like image 34
NtscCobalt Avatar answered Sep 20 '22 08:09

NtscCobalt


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.

like image 25
karlphillip Avatar answered Sep 18 '22 08:09

karlphillip