burgergroup / burger Goto Github PK
View Code? Open in Web Editor NEW:hamburger: c++11 Server based on coroutine and reactor
License: MIT License
:hamburger: c++11 Server based on coroutine and reactor
License: MIT License
The following code comes from 7ab32b4
template<typename T>
void MpscQueue<T>::enqueue(const T &input) {
BufferNode *node{new BufferNode(input)}; // #1
BufferNode *prevhead{head_.exchange(node, std::memory_order_acq_rel)}; // #2
prevhead->next_.store(node, std::memory_order_release); // #3
}
template<typename T>
bool MpscQueue<T>::dequeue(T &output) {
BufferNode *tail = tail_.load(std::memory_order_relaxed);
BufferNode *next = tail->next_.load(std::memory_order_acquire);
if (next == nullptr) {
return false;
}
output = std::move(*(next->dataPtr_));
delete next->dataPtr_; // #4
tail_.store(next, std::memory_order_release); // #5
delete tail;
return true;
}
Consider that 4 threads A, B, C, D. Assume that we have the queue with 3 elements and thread A is putting a new element 4
into the queue, in which case it's running in enqueue
. After A executes #2 successfully, the current state of the queue looks like this:
|---------------------- head
1 -> 2 -> 3 --> 4
^
|------------------ prev
Assume that A is switched out by scheduler. Now thread B, C, D are trying to dequeue elements from the queue. They run to #4 and delete 1, 2, 3 respectively.
Note that current state of prev
is freed and no longer valid. However, if A resumes at #3, the prevhead
is actually deleted by other threads and it's UB now.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.