Provides atomic operations for variables that are shared by multiple threads.
class core_export_ interlocked
static_ - Inheritance
- xtd::static_object → xtd::threading::interlocked
- Namespace
- xtd
- Library
- xtd.core
- Examples
- The following code example shows a thread-safe resource locking mechanism.
#include <vector>
#include <thread>
#include <xtd/xtd>
using namespace std;
using namespace std::chrono;
namespace examples {
struct my_thread {
std::thread thread;
};
class my_interlocked_exchange_class {
private:
inline static int using_resource = 0;
static const int num_thread_iterations = 5;
static const int num_threads = 10;
public:
static auto main() {
vector<my_thread> my_threads(num_threads);
for (
int i = 0;
i < num_threads; ++
i) {
this_thread::sleep_for(milliseconds(rnd.
next(0, 1000)));
my_threads[
i].thread = thread(my_thread_proc, my_threads[i].name);
}
for (
int i = 0;
i < num_threads; ++
i)
my_threads[i].thread.join();
}
static void my_thread_proc(
const ustring& name) {
for (
int i = 0;
i < num_thread_iterations;
i++) {
use_resource(name);
this_thread::sleep_for(milliseconds(1000));
}
}
static bool use_resource(
const ustring& name) {
this_thread::sleep_for(milliseconds(500));
return true;
} else {
return false;
}
}
};
}
startup_(examples::my_interlocked_exchange_class);
|
static int32 | add (int32 &location, int32 value) noexcept |
| Adds two 32-bit integers and replaces the first integer with the sum, as an atomic operation. More...
|
|
static int64 | add (int64 &location, int64 value) noexcept |
| Adds two 64-bit integers and replaces the first integer with the sum, as an atomic operation. More...
|
|
static double | compare_exchange (double &location, double value, double comparand) noexcept |
| Compares two Double for equality and, if they are equal, replaces one of the values. More...
|
|
static int32 | compare_exchange (int32 &location, int32 value, int32 comparand) noexcept |
| Compares two 32-bit signed integers for equality and, if they are equal, replaces one of the values. More...
|
|
static int64 | compare_exchange (int64 &location, int64 value, int64 comparand) noexcept |
| Compares two 64-bit signed integers for equality and, if they are equal, replaces one of the values. More...
|
|
static void * | compare_exchange (void *&location, void *value, void *comparand) noexcept |
| Compares two platform-specific handles or pointers for equality and, if they are equal, replaces one of them. More...
|
|
template<typename type_t > |
static type_t | compare_exchange (object &location, const object &value, const object &comparand) noexcept |
| Compares two objects for equality and, if they are equal, replaces one of them. More...
|
|
template<typename type_t > |
static type_t | compare_exchange (type_t &location, type_t value, type_t comparand) noexcept |
| Compares two instances of the specified reference type type_t for equality and, if they are equal, replaces one of them. More...
|
|
static float | compare_exchange (float &location, float value, float comparand) noexcept |
| Compares two Single for equality and, if they are equal, replaces one of the values. More...
|
|
static int32 | decrement (int32 &location) noexcept |
| Decrements a specified variable and stores the result, as an atomic operation. More...
|
|
static int64 | decrement (int64 &location) noexcept |
| Decrements a specified variable and stores the result, as an atomic operation. More...
|
|
template<typename type_t > |
static type_t | exchange (type_t &location, type_t value) |
| Sets a variable of the specified type type_t to a specified value and returns the original value, as an atomic operation. More...
|
|
static double | exchange (double &location, double value) noexcept |
| Sets a double-precision floating point number to a specified value and returns the original value, as an atomic operation. More...
|
|
static int32 | exchange (int32 &location, int32 value) noexcept |
| Sets a 32-bit signed integer to a specified value and returns the original value, as an atomic operation. More...
|
|
static int64 | exchange (int64 &location, int64 value) noexcept |
| Sets a 64-bit signed integer to a specified value and returns the original value, as an atomic operation. More...
|
|
static void * | exchange (void *&location, void *value) noexcept |
| Sets a platform-specific handles or pointers to a specified value and returns the original value, as an atomic operation. More...
|
|
template<typename type_t > |
static type_t | exchange (object &location, const object &value) noexcept |
| Sets an object to a specified value and returns the original value, as an atomic operation. More...
|
|
static float | exchange (float &location, float value) noexcept |
| Sets a double-precision floating point number to a specified value and returns the original value, as an atomic operation. More...
|
|
static int32 | increment (int32 &location) noexcept |
| Increments a specified variable and stores the result, as an atomic operation. More...
|
|
static int64 | increment (int64 &location) noexcept |
| Increments a specified variable and stores the result, as an atomic operation. More...
|
|
static void | memory_barrier () noexcept |
| Synchronizes memory access as follows: The processor that executes the current thread cannot reorder instructions in such a way that memory accesses before the call to xtd::threading::interlocked::memory_barrier execute after memory accesses that follow the call to xtd::threading::interlocked::memory_barrier. More...
|
|
static int64 | read (int64 &location) noexcept |
| Returns a 64-bit value, loaded as an atomic operation. More...
|
|