[update] thread_safe_list add get put

This commit is contained in:
dianjixz
2025-04-02 10:54:31 +08:00
parent 5b0c30f111
commit ef30b21d68
2 changed files with 9 additions and 2 deletions
+1 -1
View File
@@ -14,7 +14,7 @@ if "CONFIG_SIMDJSON_COMPENENT_ENABLED" in os.environ:
STATIC_LIB = []
DYNAMIC_LIB = []
DEFINITIONS = []
DEFINITIONS_PRIVATE = []
DEFINITIONS_PRIVATE = ['-O2']
LDFLAGS = []
LINK_SEARCH_PATH = []
DEFINITIONS += ['-std=c++17']
@@ -1,13 +1,15 @@
/*
Thread Safe Version STL in C++11
Copyright(c) 2021
Author: tashaxing
Author: tashaxing, dianjixz
update: 2025,04,02. add get put
*/
#ifndef THREAD_SAFE_LIST_H_INCLUDED
#define THREAD_SAFE_LIST_H_INCLUDED
#include <list>
#include <mutex>
#include <condition_variable>
namespace thread_safe {
@@ -117,9 +119,14 @@ public:
// Allocator
allocator_type get_allocator( void ) const { std::lock_guard<std::mutex> lock( mutex ); return storage.get_allocator(); }
T get() { std::unique_lock<std::mutex> lock(mutex); cond_var.wait(lock, [this] { return !storage.empty(); }); T value = storage.front(); storage.pop_front(); return value; }
void put(const T& value) { std::lock_guard<std::mutex> lock(mutex); storage.push_back(value); cond_var.notify_one(); }
void put(T&& value) { std::lock_guard<std::mutex> lock(mutex); storage.emplace_back(std::move(value)); cond_var.notify_one(); }
private:
std::list<T, Allocator> storage;
mutable std::mutex mutex;
std::condition_variable cond_var;
};
}