I'm going to cover a thread safety strategy I have been thinking about lately. Let's look at an example for a typical "lock the variables as you use them" approach:
class Stuff { public: void doSomething() { setString(); checkString(); } private: string myString; boost::mutex myMutex; void setString() { boost::mutex::scoped_lock l(myMutex); // Lock before touching local vars myString = "hello world"; } void checkString() { boost::mutex::scoped_lock l(myMutex); // Lock before touching local vars assert(myString == "hello world"); } };
I am proposing the following changes to the typical approach:
Pros to this method:
Cons:
This is the result of those changes. Compared it to the above example, it's a bit more concise. I think I would like to call this "Level 1 Thread Safety." By building your classes that need to be thread safe like this you can get thread safety quickly and cheaply, and have the capability to move into more efficient methods in the future if you need to.
class Stuff { public: void doSomething() { boost::mutex::scoped_lock l(myMutex); setString(myString); checkString(myString); } private: string myString; boost::mutex myMutex; void setString(string &s) { s = "hello world"; } void checkString(const string &s) { assert(s == "hello world"); } };
Recent comments
18 hours 24 min ago
23 hours 28 min ago
23 hours 56 min ago
1 day 16 hours ago
1 day 23 hours ago
1 week 6 days ago
3 weeks 17 hours ago
3 weeks 1 day ago
3 weeks 3 days ago
3 weeks 4 days ago