Boost logo

Boost :

Subject: Re: [boost] Proposal for a thread "collision" detector
From: Ivan Sorokin (vanyacpp_at_[hidden])
Date: 2013-06-18 03:22:24


On 14.06.2013 14:43, Gaetano Mendola wrote:
> I would like to know if Boost community could be interested in
> adopting a check mechanism that is able to detect if a not supposed
> thread safe class is used by multiple threads in unsafe way. This
> tecnique has saved us a lot of headache especially when code refactoring
> on old code is performed.

I used the same technique and it helped me a lot. I had a bit different
interface though.

1. Mutex-like class that asserts that there is no two threads attempt to
lock it simultaneously (similar to your DFAKE_SCOPED_LOCK lock type).

Note, that it is convenient that this class is copyable so it can be
used in copyable classes.

2. Class with the following interface:

struct thread_affinity_checker : noncopyable
{
     thread_affinity_checker();
     ~thread_affinity_checker();

     void check_current_thread() const;
};

Its constructor remembers current thread-id. Destructor and
check_current_thread() function asserts that id of current thread is the
same as remembered in constructor. This class is useful if some class
has thread affinity e.g. HWND (similar to your
DFAKE_SCOPED_LOCK_THREAD_LOCKED).

Generally I think that it's very useful to have DFAKE_SCOPED_LOCK in all
appropriate classes in boost (containers especially). Possibly for small
classes like smart-pointers, this check is costly, so it should be
enabled only with special #define. Once it took me a lot of time to
debug concurrent access to shared_ptr (assignment from it in one thread
and assignment to it in another). If this technique existed back then it
saved me a lot of time.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk