dr-m / atomic_sync Goto Github PK
View Code? Open in Web Editor NEWTiny mutex and rw-locks based on C++11 or C++20 std::atomic
License: MIT License
Tiny mutex and rw-locks based on C++11 or C++20 std::atomic
License: MIT License
In examples/atomic_condition_variable.h, the atomic_condition_variable::wait
function does this:
template<class mutex> void wait(mutex &m)
{
const uint32_t val = fetch_add(1, std::memory_order_acquire);
m.unlock();
wait(1 + val);
m.lock();
}
If the same cv object is used to wait on different conditions, the loop inside wait(1 + val);
might get waken up and see the same old value and sleep again, causing a deadlock.
Consider this code:
class single_producer_single_consumer_queue
{
private:
static constexpr std::size_t N = 8;
static constexpr std::size_t m_mask = N - 1;
using data_t = std::array<int, N>;
data_t m_data;
atomic_mutex m_mutex;
atomic_condition_variable m_cv;
std::size_t m_tail{ 0 };
std::size_t m_head{ 0 };
public:
void push(int packet)
{
std::unique_lock<atomic_mutex> lock(m_mutex);
for (;;)
{
if ((m_tail - m_head) & m_mask) != 1)
break;
m_cv.wait(lock);
}
m_data[m_head++] = packet;
m_head &= m_mask;
lock.unlock();
m_cv.signal();//notify_one
}
int pull()
{
std::unique_lock<atomic_mutex> lock(m_mutex);
for (;;)
{
if (m_head != m_tail)
break;
m_cv.wait(lock);
}
int r = m_data[m_tail++];
m_tail &= m_mask;
lock.unlock();
m_cv.signal();//notify_one
return r;
}
};
single_producer_single_consumer_queue queue;
void thread1()
{
for (int i = 0; i < 1000; ++i)
queue.push(i);
queue.push(-1);
}
void thread2()
{
int sum = 0;
for (;;)
{
int i = queue.pull();
if (i == -1)
break;
sum += i;
}
std::cout << sum << std::endl;
}
These events might happen, in this order:
queue.pull()
->m_cv.wait(lock)
->wait(1)
std::atomic<uint32_t>
inside the atomic_condition_variable
to 0 and wakes thread2std::atomic<uint32_t>
's valuestd::atomic<uint32_t>
queue.push(i)
->m_cv.wait(lock)
->wait(1)
; the underlying std::atomic<uint32_t>
is set to 1 before the blockingstd::atomic<uint32_t>
's value, finds out that it's still 1 and blocks againA 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.