concept same time in category c++

appears as: same time
Functional Programming in C++

This is an excerpt from Manning's book Functional Programming in C++.

Some parts of the saved file will come from the text as it was before the change occurred, and other parts will be from the text after it was changed. Parts of two different states will be saved at the same time.

Figure 1.2 If you allow the user to modify the text while you’re saving it, incomplete or invalid data could be saved, thus creating a corrupted file.

c01_02.png

This issue wouldn’t exist if the saving function had its own immutable copy of the data that it should write (see figure 1.3). This is the biggest problem of mutable state: it creates dependencies between parts of the program that don’t need to have anything in common. This example involves two clearly separate user actions: saving the typed text and typing the text. These should be able to be performed independently of one another. Having multiple actions that might be executed at the same time and that share a mutable state creates a dependency between them, opening you to issues like the ones just described.

Figure 1.3 If you either create a full copy or use a structure that can remember multiple versions of data at the same time, you can decouple the processes of saving the file and changing the text in the text editor.

c01_03.png

Figure 1.3 If you either create a full copy or use a structure that can remember multiple versions of data at the same time, you can decouple the processes of saving the file and changing the text in the text editor.

c01_03.png

Although you need another function to run concurrently (at the same time) with average_score for it to fail, this isn’t the only case where you might have problems because you allowed changes to the contents of the list of scores. It isn’t rare after a few years of a project’s existence to get a few variables whose values depend on each other.

Listing 6.1 Member variables of the lazy_val class template
template <typename F>
class lazy_val {
private:
    F m_computation;    #1  
    mutable bool m_cache_initialized;    #2  
    mutable decltype(m_computation()) m_cache;    #3  
    mutable std::mutex m_cache_mutex;    #4  

public:
     …

};

#1   Stores the function object that defines the computation
#2   You need to be able to tell whether you’ve already cached the computation result.
#3   Cache for the computed result. The type of the member variable is the return type of the computation.
#4   You need the mutex in order to stop multiple threads from trying to initialize the cache at the same time.