Question on using volatile in a return type

I

Ichthyostega

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Hello all,

actually I am asking as well about semantics and best practices...


To start with, I am waiting on a condition variable (in a loop that is).
The actual condition is a bool flag located elsewhere, which is
to be passed in as a function parameter. So the correct type would be
"volatile bool&"

void func1 (volatile bool& flag) {
// init...

while (!flag && !err)
err = pthread_cond_wait (&cond, &mutex);

// ...
}


The "volatile" should give the compiler a hint not to employ optimisations
but fetch the value from the original location, where it may have been changed
by another thread meanwhile -- is this correct?

And: does the flag referred to have to be declared as volatile at the original
location? (usually somewhere in a class?). Or is it sufficient to define the
reference as volatile bool& ?


Now, assumed I want to use a functor instead of the bool flag.
What would be the correct and the "best" way to define it?

class Check1 {
bool operator() () { ... }
}

class Check2 {
volatile bool operator() () { ... }
}

class Check3 {
volatile bool& operator() () { ... }
}


My understanding is that for Check3 the "volatile" is necessary, is this
correct? But Check1 should be ok, because it's returning a value and it will
actually be re-invoked in each loop iteration?

And besides, would you consider the definition within Check2 good practice,
bad practice, expressive, superfluous, ....?


Thanks
Hermann Vosseler





-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFJWBLhZbZrB6HelLIRAhvfAKDwT1b7f2sU0Mnfo6GhjCxWqUBA3QCgpzWq
Qiy/szkpY4lYukaqG7NqXPE=
=qzEq
-----END PGP SIGNATURE-----
 
P

peter koch

Hello all,
actually I am asking as well about semantics and best practices...

To start with, I am waiting on a condition variable (in a loop that is).
The actual condition is a bool flag located elsewhere, which is
to be passed in as a function parameter. So the correct type would be
"volatile bool&"

void func1 (volatile bool& flag) {
        // init...

        while (!flag && !err)
                err = pthread_cond_wait (&cond, &mutex);

        // ...

}

The "volatile" should give the compiler a hint not to employ optimisations
but fetch the value from the original location, where it may have been changed
by another thread meanwhile -- is this correct?

And: does the flag referred to have to be declared as volatile at the original
location? (usually somewhere in a class?). Or is it sufficient to define the
reference as volatile bool& ?

Now, assumed I want to use a functor instead of the bool flag.
What would be the correct and the "best" way to define it?

class Check1 {
        bool operator() () { ... }

}

class Check2 {
        volatile bool operator() () { ... }

}

class Check3 {
        volatile bool& operator() () { ... }

}

My understanding is that for Check3 the "volatile" is necessary, is this
correct? But Check1 should be ok, because it's returning a value and it will
actually be re-invoked in each loop iteration?

And besides, would you consider the definition within Check2 good practice,
bad practice, expressive, superfluous, ....?

Any use of volatile wrt threading is unneeded and bad practice (even
if you use a compiler where volatile has an established meaning
threadwise).

/Peter
 
I

Ichthyostega

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

peter said:
Any use of volatile wrt threading is unneeded and bad practice (even if you
use a compiler where volatile has an established meaning threadwise).

Hi Peter,

actually, your response confuses me somewhat. Could you please point me to
any resource explaining why this would be the case?

just to quote two:

http://www.ddj.com/cpp/184403766
http://blog.emptycrate.com/node/272

....which both seem to contradict your statement.
Probably I'm missing something very basic here?

Hermann



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFJWCDEZbZrB6HelLIRAsk0AJ44SkaHpt16o4Hd29NY4Aqjr80e2wCgpIsp
BcMTwvS4zpGEmuJsApnmTpo=
=UqZn
-----END PGP SIGNATURE-----
 
J

James Kanze

actually I am asking as well about semantics and best practices...
To start with, I am waiting on a condition variable (in a loop
that is). The actual condition is a bool flag located
elsewhere, which is to be passed in as a function parameter.
So the correct type would be "volatile bool&"
void func1 (volatile bool& flag) {
// init...
while (!flag && !err)
err = pthread_cond_wait (&cond, &mutex);
// ...
}
The "volatile" should give the compiler a hint not to employ
optimisations but fetch the value from the original location,
where it may have been changed by another thread meanwhile --
is this correct?

Formally, the meaning of "volatile" is largely implementation
dependent; the standard waffles a bit about "access", but
doesn't say what it means by an access (and the C standard says
explicitly: "What constitutes an access to an object that has
volatile-qualified type is implementation-defined.") In
practice, the compilers I know (Sun CC, g++ and VC++) don't
define it to be anything useful.

If you're waiting on a conditional variable, of course, you
don't need volatile, because all necessary synchronization has
been provided by the system requests (e.g. pthread_cond_wait,
pthread_cond_signal and pthread_cond_broadcast under Posix).
And: does the flag referred to have to be declared as volatile
at the original location? (usually somewhere in a class?). Or
is it sufficient to define the reference as volatile bool& ?

Volatile generally isn't relevant to threading. You don't need
it anywhere. (At least for the systems I'm familiar with: Posix
and Windows. A system certainly could define its threading
primitives in a way that made volatile relevant, and require
that compilers implement it in a corresponding way.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top