[LAD] Jack midi sequencer, threading and shared state

Burkhard Ritter burkhard at ualberta.ca
Sat Aug 24 22:11:01 UTC 2013

Hi all,

I just joined the developers list as I've been looking into adding
Jack midi support to one of my favourite sequencers (harmonySEQ) and
need some advice related to threading and shared data. I know these
issues have come up before, specifically I found two related threads
[1][2], but I am still confused as to what would be the best approach.
(I am also new to thread programming in general.)

As this is a sequencer the main data structure is a list of patterns
where each pattern contains a list of notes. Patterns can be
manipulated while being played back. Both the GUI thread and Jack
thread should be able to modify the patterns, e.g. a midi event might
toggle a pattern on/off, the GUI can insert a note into a pattern, or
add or remove a pattern.

I've considered different approaches (separate data for each thread,
various versions of shared data), but I believe all of them make at
least two assumptions: That reading and writing byte-sized data (e.g.
a bool flag) and that updating pointers is atomic. I got a bit
confused reading this thread [2]. Are these assumptions correct?

To be more specific, one of the schemes I came up with followed the
idea of using only one shared data structure which has to be in a
consistent state at all points in time. To ensure this I would have to
be very careful as to how I update the data structure. For example, if
the GUI added or removed a note from a pattern (which might involve a
memory reallocation) it would do so by performing theses changes on a
copy of the current pattern and then replace the original pattern by
updating the pointer to this new version of the pattern. Simple
updates (e.g. turning a pattern on / off) would be performed directly
on the pattern as I consider them atomic. To keep references to
patterns throughout the code I would use some global registry, as I
cannot use pointers for they "expire" (might point to an old version
of the pattern).

Does that make sense? Are there some obvious flaws with that scheme
that I oversaw, are there better ways to go about it? I am really
quite new to this kind of programming issues, I would appreciate any
pointers and advice!


[1] http://linux-audio.4202.n7.nabble.com/Best-practice-for-sharing-complex-data-structures-with-a-jack-callback-function-td65543.html
[2] http://linux-audio.4202.n7.nabble.com/Atomic-Operations-td58961.html

More information about the Linux-audio-dev mailing list