| Thread overview | |||||
|---|---|---|---|---|---|
|
April 03, 2007 scope(exit): exception safety article example incorrect? | ||||
|---|---|---|---|---|
| ||||
The article on exception safety (http://www.digitalmars.com/d/exception-safe.html) uses this example: void abc() { Mutex m = new Mutex; lock(m); // lock the mutex scope(exit) unlock(m); // unlock on leaving the scope foo(); // do processing } However, it seems to me this won't actually work as written. Doesn't any variable used for scoping need to be declared either static or outside the scope it's protecting? As written, it looks to me as though a new local m will be created on the stack each time a thread enters this func, which won't protect anything! TimK | ||||
April 03, 2007 Re: scope(exit): exception safety article example incorrect? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Tim Keating | Reply to Tim,
> The article on exception safety
> (http://www.digitalmars.com/d/exception-safe.html) uses this example:
>
> void abc()
> {
> Mutex m = new Mutex;
> lock(m); // lock the mutex
> scope(exit) unlock(m); // unlock on leaving the scope
> foo(); // do processing
> }
> However, it seems to me this won't actually work as written. Doesn't
> any variable used for scoping need to be declared either static or
> outside the scope it's protecting? As written, it looks to me as
> though a new local m will be created on the stack each time a thread
> enters this func, which won't protect anything!
>
> TimK
>
Good point, and I think you are correct, but I think the intent is still clear.
| |||
April 05, 2007 Re: scope(exit): exception safety article example incorrect? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Tim Keating | Tim Keating wrote:
> The article on exception safety (http://www.digitalmars.com/d/exception-safe.html) uses this example:
>
> void abc()
> {
> Mutex m = new Mutex;
>
> lock(m); // lock the mutex
> scope(exit) unlock(m); // unlock on leaving the scope
>
> foo(); // do processing
> }
>
> However, it seems to me this won't actually work as written. Doesn't any variable used for scoping need to be declared either static or outside the scope it's protecting? As written, it looks to me as though a new local m will be created on the stack each time a thread enters this func, which won't protect anything!
>
> TimK
On win32, it could be a named mutex, with the name hardcoded :P
L.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply