Implementation:CARLA simulator Carla AtomicList
| Knowledge Sources | |
|---|---|
| Domains | Concurrency, DataStructure |
| Last Updated | 2026-02-15 05:00 GMT |
Overview
AtomicList is a thread-safe list container that provides lock-free atomic reads and mutex-protected modifications using copy-on-write semantics.
Description
The AtomicList template class in carla::client::detail provides a concurrent list implementation using a copy-on-write strategy. It stores its data as a shared_ptr<vector<T>> inside an AtomicSharedPtr, enabling lock-free reads through the Load() method while protecting modifications with a std::mutex.
Key methods:
- Push(value): Acquires the mutex, creates a copy of the current list, appends the new value via emplace_back, and atomically replaces the internal pointer.
- DeleteByIndex(index): Acquires the mutex, creates a copy, erases the element at the specified index using std::advance and erase, and atomically replaces the pointer.
- DeleteByValue(value): Acquires the mutex, creates a copy, uses the erase-remove idiom to delete all matching elements, and atomically replaces the pointer.
- Clear(): Acquires the mutex and replaces the internal pointer with a new empty list.
- Load(): Returns a shared_ptr<const vector<T>> via an atomic load operation. This is the only lock-free method and provides a consistent snapshot of the list at the time of the call.
The class inherits from NonCopyable to prevent copy construction and assignment. The copy-on-write approach means that readers who obtained a snapshot via Load() continue to see a consistent view even while writers are modifying the list.
Usage
AtomicList is used internally in the CARLA client library for maintaining thread-safe collections where reads are frequent and modifications are infrequent, such as lists of registered callbacks or active subscriptions.
Code Reference
Source Location
- Repository: CARLA
- File:
LibCarla/source/carla/AtomicList.h
Signature
template <typename T>
class AtomicList : private NonCopyable {
public:
AtomicList();
template <typename ValueT>
void Push(ValueT &&value);
void DeleteByIndex(size_t index);
template <typename ValueT>
void DeleteByValue(const ValueT &value);
void Clear();
std::shared_ptr<const std::vector<T>> Load() const;
};
Import
#include "carla/AtomicList.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| value | ValueT&& | Yes (Push) | Value to append to the list |
| index | size_t | Yes (DeleteByIndex) | Zero-based index of the element to remove |
| value | const ValueT& | Yes (DeleteByValue) | Value to match and remove from the list |
Outputs
| Name | Type | Description |
|---|---|---|
| Load() | shared_ptr<const vector<T>> | Atomic snapshot of the current list contents |
Usage Examples
carla::client::detail::AtomicList<int> list;
// Thread-safe push
list.Push(42);
list.Push(100);
// Lock-free read (snapshot)
auto snapshot = list.Load();
for (const auto &item : *snapshot) {
// Process item
}
// Thread-safe removal
list.DeleteByValue(42);
list.DeleteByIndex(0);
// Clear all entries
list.Clear();