I need a simple non-blocking memory pool the size of a static block. I did not find this on the Internet. So everyone who needs such a solution. This free ... only works with Win32.
Yours faithfully,
Friedrich
And CompareAndSwap
/// @brief Atomic compare and set /// Atomically compare the value stored at *p with cmpval and if the /// two values are equal, update the value of *p with newval. Returns /// zero if the compare failed, nonzero otherwise. /// @param p Pointer to the target /// @param cmpval Value as we excpect it /// @param newval New value static inline int CompareAndSwap(volatile int *_ptr, int _old, int _new) { __asm { mov eax, [_old] // place the value of _old to EAX mov ecx, [_new] // place the value of _new to ECX mov edx, [_ptr] // place the pointer of _ptr to EDX lock cmpxchg [edx], ecx // cmpxchg old (EAX) and *ptr ([EDX]) } return 1; }
c ++ thread-safety nonblocking memory-pool
Friedrich
source share