demetri
Premium
I tried Bing AI chat today. I asked it to write C++ code that implements a lock-free priority queue (not an easy task, mind you, but it is doable). To my surprise, it actually came up with some valid C++ code, but the code had quite a few glaring race conditions that I pointed it to, it apologized, fixed some of them, but not all, to which I pointed again, so it tried again and still fell short and that cycle continued until we ran out of the chat depth limit of 20 questions. Some of intermediate results it came up with were pretty hilarious, e.g. there was a check if a vector's number of elements was more than one (with some code to execute if true) and then in the 'else' clause of that it tried to assign something to the second element of that vector (which was guaranteed to have no more than one element in that branch of the code).
My subsequent attempts to solve this in a new chat sessions failed, as it refused to produce any C++ code itself anymore, instead providing me with links to github code or some whitepapers about the problem. I finally managed to make it write a simple (not priority) lock-free queue code in C++, but that code just doesn't work, pushing a value into an empty queue doesn't modify the head, so front() returns garbage instead of the value you just pushed there:
I think you might use that if you need some simple boilerplate code that you're too lazy to write yourself and for some reason it's not available as a library, but you'll have to double-check that the code works and anything remotely complex is still out of reach, so I don't expect any miracles here.
My subsequent attempts to solve this in a new chat sessions failed, as it refused to produce any C++ code itself anymore, instead providing me with links to github code or some whitepapers about the problem. I finally managed to make it write a simple (not priority) lock-free queue code in C++, but that code just doesn't work, pushing a value into an empty queue doesn't modify the head, so front() returns garbage instead of the value you just pushed there:
Code:
#include <atomic>
template <typename T>
class Queue {
private:
struct Node {
T data;
std::atomic<Node*> next;
};
std::atomic<Node*> head;
std::atomic<Node*> tail;
public:
Queue() : head(nullptr), tail(nullptr) {
Node* node = new Node{T(), nullptr};
head.store(node);
tail.store(node);
}
~Queue() {
while (Node* node = head.load()) {
head.store(node->next.load());
delete node;
}
}
void push(const T& data) {
Node* node = new Node{data, nullptr};
Node* prev_tail = tail.exchange(node);
prev_tail->next.store(node);
}
void pop() {
Node* old_head = head.load();
Node* new_head = old_head->next.load();
while (head.load() != old_head) {
old_head = head.load();
new_head = old_head->next.load();
}
if (old_head == tail.load()) {
return;
}
head.store(new_head);
delete old_head;
}
bool empty() const {
return head.load() == tail.load();
}
const T& front() const {
return head.load()->data;
}
};
I think you might use that if you need some simple boilerplate code that you're too lazy to write yourself and for some reason it's not available as a library, but you'll have to double-check that the code works and anything remotely complex is still out of reach, so I don't expect any miracles here.
Last edited: