can you a lock free task queue?
- User
can you a lock free task queue?
- Assistant
struct Task { std::function<void()> func; };
struct TaskNode { Task task; std::atomic<TaskNode*> next; };
class TaskQueue { public: TaskQueue() : head(nullptr), tail(nullptr) {}
void PushTask(const Task& task) { TaskNode* newNode = new TaskNode{task, nullptr}; std::atomic<TaskNode**>::compare_exchange_strong( &tail->next, &newNode, nullptr, std::memory_order_release); } void PopTask() { TaskNode* oldTail = nullptr; do { oldTail = tail.load(std::memory_order_acquire); } while (!std::atomic_compare_exchange_strong( &tail, &oldTail, oldTail->next, std::memory_order_release, oldTail->next).success()); if (oldTail == nullptr) { return; } Task task = oldTail->task; delete oldTail; }private: std::atomic<TaskNode*> head; std::atomic<TaskNode*> tail; };
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.