Re: Fast lock-based FIFO alias bounded buffer
"Szabolcs Ferenczi" <szabolcs.ferenczi@gmail.com> wrote in message
news:cc210284-3c9b-4b55-b99e-5bb1657e0dc1@e10g2000prf.googlegroups.com...
I was re-reading the concurrent language tools of the Edison language,
when one interesting note caught my eyes: "... But the programmer is
no longer tied to the monitor concept, but can use simpler concepts,
such as semaphores ... Semaphores can then be used to implement a
multislot buffer in which sending and receiving can take place
simultaneously from different slots"
It seems that a solution does not necessarily have to be lock-free in
order to be efficient.
[...]
Efficient lock-based solutions are great, thanks for posting this; I am
looking forward to examining it. BTW, have you checked out Joe Seighs
semaphore algorithm which has wait-free fast-paths? They would definitely be
an asset to your algorithm. IIRC, here is how it went (in windows):
<sketch of 'joe_seigh_sem.hpp' which should compile>
___________________________________________________________________
#if ! defined(JOE_SEIGH_SEM_INCLUDE_HPP)
# define JOE_SEIGH_SEM_INCLUDE_HPP
# include <windows.h>
# include <exception>
# include <cassert>
# include <climits>
# define JOE_SEIGH_SEM_UNEXPECTED() \
assert(false); std::terminate()
/*===========================================================*/
class joe_seigh_sem {
LONG m_state;
HANDLE m_wset;
public:
joe_seigh_sem(LONG CONST state = 0)
: m_state(state),
m_wset(CreateSemaphore(NULL, 0, LONG_MAX, NULL)) {
if (! m_wset || state < 0) {
if (m_wset) { CloseHandle(m_wset); }
throw std::exception();
}
}
~joe_seigh_sem() throw() {
if (! CloseHandle(m_wset)) {
JOE_SEIGH_SEM_UNEXPECTED();
}
}
public:
void post() throw() {
if (InterlockedIncrement(&m_state) < 1) {
if (! ReleaseSemaphore(m_wset, 1, NULL)) {
JOE_SEIGH_SEM_UNEXPECTED();
}
}
}
void wait() throw() {
if (InterlockedDecrement(&m_state) < 0) {
if (WaitForSingleObject(m_wset, INFINITE) !=
WAIT_OBJECT_0) {
JOE_SEIGH_SEM_UNEXPECTED();
}
}
}
};
/*===========================================================*/
#endif
___________________________________________________________________
what do you think?