On Fri, 6 Nov 2020 17:20:04 +0100 "Jörg F. Wittenberger" <[email protected]> wrote: > Am Thu, 05 Nov 2020 23:22:09 +0100 > schrieb Fredrik Appelberg <[email protected]>: > > > 3. I'm new to dynamic-wind. If I wanted to create a general form for > > executing a thunk protected by a mutex, would this be a good idea? > > > > (define (with-lock mutex thunk) > > (dynamic-wind > > (lambda () (mutex-lock! mutex)) > > thunk > > (lambda () (mutex-unlock! mutex))))) > > > > I read somewhere that the before- and after-guards might execute > > multiple times, but then again I'm not really sure under what > > circumstances so I might be way off. > > This approach is bound to fail badly. > > It works just as long as there are a) no exceptions raised in `thunk` > b) no code, not even in a library does any `call/cc`. Including > `call/cc` hidden in exception handlers (srfi-12, srfi-34 etc.)
For my elucidation, why? The indentation of the code isn't ideal but the whole purpose of dynamic-wind is to handle code leaving the thunk in case of exception, application of a continuation object or regular return. Admittedly it would be more usual for this to be implemented as a macro rather than a function with a thunk, but that is a minor matter.
