### Concurrent Wrapper Constructors (C++) Source: https://context7.com/xmkg/concurrent-resource/llms.txt Demonstrates how to construct a concurrent wrapper. Supports default construction, copy construction from an existing value, and move construction, leveraging the capabilities of the wrapped type. ```cpp #include "concurrent_stl.hpp" #include #include #include int main() { // Default construction mkg::concurrent> vec1; // Copy construction from existing value std::vector initial_books = {"Dune", "Foundation"}; mkg::concurrent> vec2(initial_books); // Move construction std::vector temp_data = {1.1, 2.2, 3.3}; mkg::concurrent> vec3(std::move(temp_data)); { auto reader = vec2.read_access_handle(); std::cout << "Books: "; for(const auto& book : (*reader)) { std::cout << book << " "; } std::cout << "\n"; } return 0; } ``` -------------------------------- ### C++17 If-Init for Conditional Access Source: https://context7.com/xmkg/concurrent-resource/llms.txt Demonstrates C++17's if-statement with initializer syntax for concise accessor lifetime management and immediate lock release. This pattern simplifies conditional operations on concurrently accessed resources. ```cpp #include "concurrent_stl.hpp" #include #include #include int main() { mkg::concurrent> tasks; // C++17: if statement with initializer if(auto writer = tasks.write_access_handle(); writer->empty()) { writer->push_back("Initialize"); writer->push_back("Configure"); std::cout << "Added initial tasks\n"; } // Lock released immediately after if-block // Another conditional access if(auto reader = tasks.read_access_handle(); !reader->empty()) { std::cout << "First task: " << (*reader)[0] << "\n"; std::cout << "Total tasks: " << reader->size() << "\n"; } // Traditional scope-based pattern for comparison { auto writer = tasks.write_access_handle(); if(!writer->empty()) { writer->push_back("Execute"); } } return 0; } ``` -------------------------------- ### User-Defined Types with Concurrent Wrapper (C++) Source: https://context7.com/xmkg/concurrent-resource/llms.txt Illustrates wrapping custom C++ structures and classes with the concurrent wrapper. This demonstrates the library's compatibility with arbitrary user-defined types, not limited to standard library containers. ```cpp #include "concurrent_stl.hpp" #include #include #include #include #include struct UserConfig { std::array buffer; float coefficient = 0.1f; std::map settings; void initialize() { buffer.fill(0); settings[1] = "enabled"; settings[2] = "debug"; } }; int main() { // Direct wrapping of user-defined type mkg::concurrent config; { auto writer = config.write_access_handle(); writer->initialize(); writer->coefficient = 0.95f; writer->settings[3] = "production"; std::cout << "Coefficient: " << writer->coefficient << "\n"; std::cout << "Settings count: " << writer->settings.size() << "\n"; } // Wrapping smart pointers to user-defined types std::unique_ptr> config_ptr; config_ptr = std::make_unique>(); { auto writer = config_ptr->write_access_handle(); writer->coefficient = 0.75f; std::cout << "Modified coefficient: " << writer->coefficient << "\n"; } return 0; } ``` -------------------------------- ### Concurrent Wrapper for Pointer-Type Resources Source: https://context7.com/xmkg/concurrent-resource/llms.txt Demonstrates wrapping smart pointers (e.g., std::unique_ptr) as the concurrent resource. Accessing the underlying resource requires dereferencing the pointer, and member access needs double indirection. ```cpp #include "concurrent_stl.hpp" #include #include #include struct DataBuffer { std::array data; size_t size = 0; void append(uint8_t byte) { if(size < data.size()) { data[size++] = byte; } } }; int main() { // Concurrent wrapper around a unique_ptr mkg::concurrent> buffer_ptr; { auto writer = buffer_ptr.write_access_handle(); // Initialize the pointer (*writer) = std::make_unique(); // Or: writer->reset(new DataBuffer()); // Access members requires double indirection (*writer)->append(0xFF); (*writer)->append(0xAB); (*writer)->data[0] = 0x42; std::cout << "Buffer size: " << (*writer)->size << "\n"; std::cout << "First byte: 0x" << std::hex << (int)(*writer)->data[0] << "\n"; } { auto reader = buffer_ptr.read_access_handle(); if(*reader) { std::cout << "Buffer contains " << (*reader)->size << " bytes\n"; } } return 0; } ``` -------------------------------- ### Custom Lock Types with Concurrent Wrapper Source: https://context7.com/xmkg/concurrent-resource/llms.txt Illustrates using the generic concurrent wrapper with custom lock types like std::shared_mutex. The library supports any lock implementation satisfying basic_lockable and basic_shared_lockable concepts. ```cpp #include "concurrent.hpp" #include #include #include #include int main() { // Explicitly specify lock types (normally done via concurrent_stl.hpp or concurrent_boost.hpp) mkg::concurrent< std::vector, std::shared_mutex, std::shared_lock, std::unique_lock > numbers; { auto writer = numbers.write_access_handle(); for(int i = 0; i < 5; ++i) { writer->push_back(i * 10); } } { auto reader = numbers.read_access_handle(); std::cout << "Numbers: "; for(int num : (*reader)) { std::cout << num << " "; } std::cout << "\n"; } return 0; } ``` -------------------------------- ### Create Thread-Safe Vector with Read-Write Accessors in C++ Source: https://github.com/xmkg/concurrent-resource/blob/master/README.md Demonstrates wrapping a standard vector with mkg::concurrent to make it thread-safe. Shows acquiring write accessors for exclusive modification and read accessors for concurrent read-only access. Locks are automatically managed through accessor object lifetime via RAII pattern. ```cpp #include "concurrent_stl.hpp" // or concurrent_boost.hpp #include #include // A very-basic example int main(void){ // Wrap a regular vector to make it thread-safe mkg::concurrent> books; { // Grab a write accessor (we can manipulate the vector through it) auto writer = books.write_access_handle(); // Now, an exclusive lock is held upon the construction of write accessor // by std::unique_lock(...) (or boost::) // We can reach the underlying vector by just ->'ing the writer. writer->push_back("White Fang"); writer->push_back("Harry Potter and the Philosopher's Stone"); // Attention: Do not attempt to acquire another write accessor from same thread whilst // another one is already alive. } // Here, right after the accessor object is destroyed (by going out of scope) // the lock is released. { // Grab a read accessor (read-only, cannot modify the vector through it) auto reader = books.read_access_handle(); // Treat the accessors as if they're pointers to the underlying resource // (or std|boost::optional's if you will) for(const auto & book : (*reader)){ std::cout << book << std::endl; } } } ``` -------------------------------- ### Producer-Consumer Pattern with Concurrent Wrappers (C++) Source: https://context7.com/xmkg/concurrent-resource/llms.txt Implements a thread-safe producer-consumer pattern using concurrent wrappers. This allows multiple threads to produce and consume data concurrently without manual mutex management, relying on the library's synchronization mechanisms. ```cpp #include "concurrent_stl.hpp" #include #include #include #include #include #include int main() { mkg::concurrent> shared_queue; std::atomic running{true}; std::atomic msg_id{0}; // Producer thread std::thread producer([&]() { for(int i = 0; i < 10; ++i) { { auto writer = shared_queue.write_access_handle(); std::string id = std::to_string(msg_id++); writer->emplace(id, "Message_" + id); std::cout << "Produced: " << id << "\n"; } std::this_thread::sleep_for(std::chrono::milliseconds(100)); } running = false; }); // Consumer thread std::thread consumer([&]() { while(running || true) { { auto reader = shared_queue.read_access_handle(); if(!reader->empty()) { for(const auto& [key, value] : (*reader)) { std::cout << "Consumed: " << key << " -> " << value << "\n"; } } } { auto writer = shared_queue.write_access_handle(); if(!writer->empty()) { writer->erase(writer->begin()); } if(!running && writer->empty()) break; } std::this_thread::sleep_for(std::chrono::milliseconds(150)); } }); producer.join(); consumer.join(); return 0; } ``` -------------------------------- ### Accessor Member Access (C++) Source: https://context7.com/xmkg/concurrent-resource/llms.txt Shows how the `operator->` on an accessor provides direct member access to the underlying resource, such as a `std::map`. This allows calling member functions like `insert`, `emplace`, `operator[]`, and `find` on the wrapped object as if it were accessed directly, while still maintaining thread safety. ```cpp #include "concurrent_stl.hpp" #include #include #include int main() { mkg::concurrent> scores; { auto writer = scores.write_access_handle(); // Use -> to call map member functions writer->insert({"Alice", 95}); writer->emplace("Bob", 87); writer->operator[]("Charlie") = 92; std::cout << "Map size: " << writer->size() << "\n"; auto it = writer->find("Alice"); if (it != writer->end()) { std::cout << "Alice's score: " << it->second << "\n"; } } return 0; } ``` -------------------------------- ### Accessor Dereference (C++) Source: https://context7.com/xmkg/concurrent-resource/llms.txt Demonstrates using the `operator*` to dereference the accessor and obtain a reference to the underlying resource. This is useful for modifying primitive types or string content, or when a direct reference to the entire object is needed. Read-only accessors prevent modification through the dereferenced reference. ```cpp #include "concurrent_stl.hpp" #include #include int main() { mkg::concurrent message; { auto writer = message.write_access_handle(); // Use * to get reference to the string (*writer) = "Hello, concurrent world!"; (*writer) += " This is thread-safe."; std::cout << "Length: " << (*writer).length() << "\n"; } { auto reader = message.read_access_handle(); // Read-only access via dereference std::cout << "Message: " << (*reader) << "\n"; // (*reader) = "Cannot modify"; // Compile error: read-only } return 0; } ``` -------------------------------- ### Write Access to Concurrent Vector (C++) Source: https://context7.com/xmkg/concurrent-resource/llms.txt Demonstrates acquiring exclusive write access to a concurrent vector of strings. The `write_access_handle` returns an accessor that locks the resource exclusively, allowing modifications. The lock is automatically released when the accessor goes out of scope, ensuring safety. ```cpp #include "concurrent_stl.hpp" #include #include #include int main() { mkg::concurrent> books; { // Acquire exclusive write access auto writer = books.write_access_handle(); // Modify the underlying vector through the accessor writer->push_back("White Fang"); writer->push_back("Harry Potter and the Philosopher's Stone"); writer->emplace_back("The Great Gatsby"); std::cout << "Added " << writer->size() << " books\n"; } // Lock automatically released when accessor goes out of scope return 0; } ``` -------------------------------- ### Read Access to Concurrent Vector (C++) Source: https://context7.com/xmkg/concurrent-resource/llms.txt Illustrates obtaining shared read-only access to a concurrent vector of strings. Multiple threads can simultaneously hold read accessors via `read_access_handle`, which uses a shared lock. This allows concurrent reads without blocking other readers, enhancing performance for read-heavy workloads. ```cpp #include "concurrent_stl.hpp" #include #include #include #include int main() { mkg::concurrent> books; // Initialize data { auto writer = books.write_access_handle(); writer->push_back("1984"); writer->push_back("Brave New World"); } // Multiple threads can read concurrently auto read_thread = [&books]() { auto reader = books.read_access_handle(); for(const auto& book : (*reader)) { std::cout << "Thread " << std::this_thread::get_id() << " reads: " << book << "\n"; } }; std::thread t1(read_thread); std::thread t2(read_thread); t1.join(); t2.join(); return 0; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.