|
Boost Users : |
From: Peter Dimov (pdimov_at_[hidden])
Date: 2005-04-27 05:39:40
Ross Manges wrote:
>> I'm not seeing any violations of the rules in your code; the
>> problem is probably more mundane:
> I moved the mutex as suggested, and the program runs without
> incident now. However, I have more examples. I made 'retElem' a
> member variable in the class SimpleQueue. This probably (?) breaks
> the shared_ptr rules for exclusivity. Nonetheless, the program
> still runs OK. But, I also tried a version of the program using
> CommonCPP threads and mutexes instead of Boost threads and
> mutexes, and it dies and dumps a core.
Your problems are getting more subtle. The Boost threads version is fine,
because pop looks like this in pseudocode:
lock
write retElem
return retElem
implicit unlock
Since all accesses to retElem are protected, this doesn't violate the rules.
But the CommonC++ version is:
lock
write retElem
unlock
return retElem
and as you can see, another thread may enter the critical region and write
retElem in parallel with the return statement, which needs to make a copy of
retElem and place it in the return value.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net