RE: [boost] [shared_ptr] InterlockedIncrement rocks!

Tomas Puverle <Tomas.Puverle@morganstanley.com> wrote:
Also, why not including windows.h directly, like this: #define WIN32_LEAN_AND_MEAN #include <windows.h>
I believe WIN32_EXTRA_LEAN may be even better.
<side-note> From my tests it seems your version as is now fails to link for gcc (unresolved external _InterlockedIncrement/_InterlockedDecrement). However, if #including <windows.h> and use InterlockedIncrement/Decrement - they work. </side-note>
That is not suprising - Interlocked* are parts of the Win32 API.
In fact, it has been a complete mistery to me as to why boost doesn't use atomic operations for its ref counting. The mutex on the counter was one of the big reasons why people here decided not to use shared_ptr.
Atomic operations are highly platform-dependent, with different platforms having different capabilities and many requiring use of assembly language rather than providing an API for atomic operations. Second, shared_ptr has two counters and updating them safely without using a lock requires some subtlety. It's something I meant to implement but hadn't quite got round to. Perhaps I could make it work on g++ (and hence most Linux configurations) by using libstdc++'s atomic primitives? I don't think they are really public though, so this might be a bad idea.
participants (1)
-
Ben Hutchings