Jens Gustedt's Blog

January 28, 2011

linux futexes: non-blocking integer valued condition variables

Filed under: C99, language, linux, lock structures, POSIX — Jens Gustedt @ 19:57

POSIX’ condition variables pthread_cond_t unfortunately have some drawbacks:

  • They require the use of at least two separate other objects that are only loosely coupled with the condition variable itself:

    • A plain variable, say X of type int, that actually holds the value of which the condition depends.
    • A mutex that is just to regulate the access to X and to the condition variable.
  • Generally they are not lock free. To access X we have to

    • lock the mutex
    • inspect X
    • eventually wait for the condition to become true
    • do changes
    • eventually signal other waiters about the changes we made
    • unlock the mutex

Linux’ concept of futexes allows to associate an equivalent concept directly to the variable X and to access this variable without taking locks directly in most of the cases.
(more…)

Advertisements

January 13, 2011

VLA as function arguments

Filed under: C99, language, P99 — Jens Gustedt @ 22:05

Now that we aren’t afraid of variably modified types anymore let us have a look on how these can be used as function arguments.

Therefore we will

(more…)

January 9, 2011

don’t be afraid of variably modified types

Filed under: C99, language, P99, syntax — Jens Gustedt @ 14:47

This post is about a subject that easily triggers a lot of polemic and diverging opinions, so I will try to be careful to get things right. In any case the message of this post may be summarized as:

  • Plain variable length arrays (VLA) might be dangerous for your health, since they may cause stack overflows, and generally are not so easy to handle
  • In constrast to that pointers to VLA are a quite different story and may ease the programming with multi-dimensional arrays substantially.

(more…)

Create a free website or blog at WordPress.com.