celix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gerrit Binnenmars (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (CELIX-233) Replace booleans as sync primitives
Date Tue, 19 May 2015 07:15:01 GMT

    [ https://issues.apache.org/jira/browse/CELIX-233?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14549955#comment-14549955

Gerrit Binnenmars commented on CELIX-233:

Bjoern, in principal you are right, see e.g. http://stackoverflow.com/questions/19534072/why-to-use-mutex-instead-of-boolean-variable-for-thread-synchronization
So in all cases where we have
if (bool == false)
     bool = true;
a mutex shall be used.

If we have a global variable that indicates a program is still running, I don't think adding
a mutex adds much. In this case I assume we set/reset the variable in one location and test
it in multiple locations.

> Replace booleans as sync primitives
> -----------------------------------
>                 Key: CELIX-233
>                 URL: https://issues.apache.org/jira/browse/CELIX-233
>             Project: Celix
>          Issue Type: Bug
>            Reporter: Bjoern Petri
>            Assignee: Bjoern Petri
> In a lot of cases we use boolean variables (e.g. bool running) to determine whether a
critical section can be executed. Usually those variables are not declared volatile, so the
they may be cached in registers. Before the value in the register is written to memory, another
thread might be scheduled to run, resulting in that thread reading stale data.
> But even when declaring a variable as volatile (although the volatile qualifier guarantees
that the reads and writes will happen in the exact order specified in the source code), the
compiler may generate code which reorders a volatile read or write with non-volatile reads
or writes, thus limiting its usefulness as an interthread flag or mutex.
> That's why it should be preferable to use e.g. a mutex as sync primitive

This message was sent by Atlassian JIRA

View raw message