Imagine that we are designing a safety-critical class that is used from multiple threads, receives answers from a server, postprocesses them, and outputs the response:
struct postprocessor { typedef std::vector<std::string> answer_t; // Concurrent calls on the same variable are safe. answer_t act(const std::string& in) const { if (in.empty()) { // Extremely rare condition. return read_defaults(); } // ... } };
Note the return read_defaults();
line. There may be situations when server does not respond because of networking issues or some other problems. In those cases, we attempt to read defaults from file:
// Executes for a long time. std::vector<std::string> read_defaults();
From the preceding code, we hit the problem: the server may be unreachable for some noticeable time, and for all that time we'll be rereading the file on each act
call. This significantly affects performance...