Re: [Vala] volatile variable



On Mon, 2014-06-16 at 08:44 +0800, Nor Jaidi Tuah wrote:
As a side question - why do you need volatile? In most cases it's not
needed (unless you write kernel/driver and do memory based I/O).

My multithreaded code didn't work and I thought
may be gcc is making a wrong optimization.
Turns out to be my own fault.

But still, I'm curious, can gcc make a wrong
optimization not knowing that a variable may
be changed by another thread?

I notice that both glib and vala use volatile ref_count.
So, if we were to do our own application-specific
memory management, shouldn't we have volatile
somewhere?


Nice day
Nor Jaidi Tuah


Well the volatile ref_count is not accessed as volatile - it's accessed
as atomic via GLib.Atomic.int_add and similar functions. If you want to
use atomics see libgee code for usage[1] (yes, I know - shameless
self-promotion) - note that atomic code can become hard easily.

Best regards

[1] Note that because the libgee deals with pointers it needs to
implement a bit more. If you need a guide see
http://blog.piechotka.com.pl/2014/03/01/lock-free-collection-in-libgee-hazard-pointer/ (even more 
self-promotion)

Attachment: signature.asc
Description: This is a digitally signed message part



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]