Atomic Operations | wxWidgets

| Version: 3.2.11

Loading...
Searching...
No Matches
Functions
Atomic Operations
Functions and Macros by Category

## Detailed Description

When using multi-threaded applications, it is often required to access or modify memory which is shared between threads.

Atomic integer and pointer operations are an efficient way to handle this issue (another, less efficient, way is to use a wxMutex or wxCriticalSection). A native implementation exists for Windows, Linux, Solaris and macOS; for others, a wxCriticalSection is used to protect the data.

One particular application is reference counting (used by so-called smart pointers).

You should define your variable with the type wxAtomicInt in order to apply atomic operations to it.

## Functions
wxInt32 | wxAtomicInc (wxAtomicInt &value)
| This function increments value in an atomic manner.
wxInt32 | wxAtomicDec (wxAtomicInt &value)
| This function decrements value in an atomic manner.

## Function Documentation

## ◆ wxAtomicDec()

wxInt32 wxAtomicDec | ( | wxAtomicInt & | value | ) |

This function decrements value in an atomic manner.

Returns the new value after decrementing it.

See also
  wxAtomicInc

Include file:

    #include <wx/atomic.h>

## ◆ wxAtomicInc()

wxInt32 wxAtomicInc | ( | wxAtomicInt & | value | ) |

This function increments value in an atomic manner.

Note
  It is recommended to use `std::atomic` available in C++11 and later instead of this function in any new code.

Whenever possible wxWidgets provides an efficient, CPU-specific, implementation of this function. If such implementation is available, the symbol wxHAS_ATOMIC_OPS is defined. Otherwise this function still exists but is implemented in a generic way using a critical section which can be prohibitively expensive for use in performance-sensitive code.

Returns the new value after the increment (the return value is only available since wxWidgets 3.1.7, this function doesn't return anything in previous versions of the library).

Include file:

    #include <wx/atomic.h>