xtd 0.2.0
Loading...
Searching...
No Matches

◆ is_synchronized()

template<typename type_t , typename allocator_t = xtd::collections::generic::helpers::allocator<typename std::conditional<std::is_same<bool, type_t>::value, char, type_t>::type>>
bool xtd::collections::generic::list< type_t, allocator_t >::is_synchronized ( ) const
inlineoverridevirtualnoexcept

Gets a value indicating whether access to the xtd::collections::generic::icollection <type_t> is synchronized (thread safe).

Returns
true if access to the xtd::collections::generic::icollection <type_t> is synchronized (thread safe); otherwise, false.
Remarks
xtd::collections::generic::icollection::sync_root returns an object, which can be used to synchronize access to the xtd::collections::generic::icollection <type_t>.
Most collection classes in the xtd::collections namespace also implement a synchronized method, which provides a synchronized wrapper around the underlying collection.
Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
The following code example shows how to lock the collection using the xtd::collections::generic::icollection::sync_root property during the entire enumeration.
icollection& my_collection = some_collection;
lock_(my_collection.sync_root()) {
for (auto item : my_collection) {
// Insert your code here.
}
}
virtual const xtd::object & sync_root() const noexcept=0
Gets an object that can be used to synchronize access to the the xtd::collections::generic::icollecti...
Defines methods to manipulate generic collections.
Definition icollection.h:44
#define lock_(object)
The lock_ keyword marks a statement block as a critical section by obtaining the mutual-exclusion loc...
Definition lock.h:85

Implements xtd::collections::generic::icollection< type_t >.