user manual code example race condition

Pascal Bourguignon pjb at informatimago.com
Mon Mar 11 15:18:09 UTC 2019



> On 11 Mar 2019, at 10:59, Mark Evenson <evenson at panix.com> wrote:
> 
> 
> 
>> On Mar 11, 2019, at 06:38, dingd <ntysdd at qq.com> wrote:
>> 
>> this is recommended in the manual, it contains a race condition.
>> 
>> Interpreter interpreter = Interpreter.getInstance ();
>> if ( interpreter == null ) {
>> 	interpreter = Interpreter.createInstance ();
>> }
>> 
>> getInstance and createInstance should be swapped.
> 
> Since both Interpreter.getInstance() and Interpreter.createInstance() are
> methods synchronized on the same object monitor, I don’t see that there is a
> race condition here.  Could you explain a little more about your reasoning
> and/or experience with the race condition?



Two threads could call getInstance and have interpreter=null.
Then both will call createInstance, thus two instances will be created.
If interpreter is a local binding as it appears in the code above, then it is what’s intended and all is good.
But if interpreter is a global binding and you expected interpreter to have a single instance, then it’s wrong.


-- 
__Pascal J. Bourguignon__




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/armedbear-devel/attachments/20190311/ab1d8ca9/attachment.html>


More information about the armedbear-devel mailing list