celix-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bjoern Petri <bjoern.pe...@sundevil.de>
Subject replace booleans as sync primitives
Date Wed, 20 May 2015 06:02:47 GMT

Hi everyone,

For several of our bundles we use the same approach to control 
additional threads within
a bundle:

A boolean variable is used to indicate whether the thread is supposed to 
run. The
according thread function regularly checks whether this variable is set 
threadIsRunning == true) and returns when set to false.  When stopping 
the bundle
we usually set this variable to false followed by a pthread_join.

This approach seems to have some flaws:

- Usually the boolean variables are not declared volatile. This should 
be done to
guarantee that the write has actually happened before another read. In 
case this
is not done, the thread might still be executed although the global 
indicates to not do so. This might lead to a problem when we invalidate 
data after
setting the global variable (and before doing a pthread_join).

- Even when having the boolean variables declared volatile, the compiler 
may re-order
the code as a result of compiler optimizations. So, data might be 
invalidated before
the boolean variable is set to false.

That's why it may be preferable to replace the sync primitive for 
threading with an
appropriate approach.

While this issue has already been commented on JIRA, it might be useful 
to gather some
more opinions on this issue and how it can be solved best. Any comments 
are welcome.


See also:

View raw message