Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.os.linux.development.apps > #345

Re: Blocking queue race condition?

From Rainer Weikusat <rweikusat@mssgmbh.com>
Newsgroups comp.os.linux.development.apps
Subject Re: Blocking queue race condition?
Date 2012-01-05 20:39 +0000
Message-ID <87vcopnau3.fsf@sapphire.mobileactivedefense.com> (permalink)
References <74ec83e6-eafc-440e-bb60-8a1ed6412811@h13g2000vbn.googlegroups.com>

Show all headers | View raw


> I'm trying to implement a high performance blocking queue backed by a
> circular buffer on top of pthreads, semaphore.h and gcc atomic
> builtins.  The queue needs to handle multiple simulataneous readers
> and writers from different threads.
>
> I've isolated some sort of race condition, and I'm not sure if it's a
> faulty assumption about the behavior of some of the atomic operations
> and semaphores, or whether my design is fundamentally flawed.

I've had a look at this and did some experiments with it (specially, I
ran it with one thread get and one set thread which seemed to work and
two set and one get thread, corruption occuring in get, and two get
and one set thread, corruption in set) and - based on the observation
that there is no ordering imposed in which two threads of the same
kind perform their operations, I think what's happening here is as
follows:

Let's assume that there are two get threads and one set
thread. Initially, head == tail and there are at least two occupants
in the queue. Then, the following scenario is possible:

	1. get0 returns from sem_wait, does sync_fetch_and for
	position 0.

        2. get1 returns from sem_post, does sync_fetch... for position
        1.

        3. get1 executes CAS, then posts vacancies.

        4. set returns from sem_wait, does sync_and_fetch for position 0.

        5. set executes CAS before get0, detecting
        'corruption'.

        6. some time after 5) get0 does CAS.

NB: This is a hypothesis/ theory which seems plausible to
me. Corrections etc appreciated.

Back to comp.os.linux.development.apps | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Blocking queue race condition? tomazos <tomazos@gmail.com> - 2012-01-04 22:08 -0800
  Re: Blocking queue race condition? Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-01-05 20:39 +0000
  Re: Blocking queue race condition? tomazos <andrew@tomazos.com> - 2012-01-05 14:01 -0800

csiph-web