2

I have some global variables that I need to share on multi source files. I have read How do I share variables between different .c files? and do as following:

fileA.h:

extern int a;

fileA.c:

int a;
// using a variable here

Everything compile well and work fine. But because I program on multithread environment, so I put volatile keyword before each variable. so now is :

fileA.h:

extern volatile int a;

fileA.c:

int a;
// using a variable here

But when I use this way, I meet error when compiling:

error: conflicting type qualifiers for ‘a’ in file included from fileA.c:4:0:

Please explain for me why, and how to fix this? Should we use volatile in this case?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
hqt
  • 29,632
  • 51
  • 171
  • 250
  • You need to put the volatile modifier on all declarations and definitions of the same extern variable. For a discuss on volatile see this [stackoverflow: the volatile keyword in C language](http://stackoverflow.com/questions/5822386/the-volatile-keyword-in-c-language) – Richard Chambers Apr 13 '14 at 03:32

1 Answers1

2

Any declarations for a variable should agree with its actual definition. So if the declarations in the include files for extern volatile int a; should have an associated volatile int a; in the file where the variable is actually defined.

Also see this article How to use C's volatile keyword including some information about multi-threaded applications.

Be aware that with multi-thread applications you may need to use some kind of locking or other mutual exclusion mechanism with a shared variable. See Why is volatile not considered useful in multi-threaded C or C++ programs

The main thing that the volatile keyword does is to inform the compiler that a variable may change from some action outside of the current scope so it affects how the compiler goes about generating machine code.

See this brief article Compiler optimization and the volatile keyword.

And see this Dr. Dobbs article volatile: The Multithreaded Programmer's Best Friend

Community
  • 1
  • 1
Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
  • @ : I meet this warning when compile with `volatile` keyword before pthread_mutex_t variable : `passing argument 1 of ‘pthread_mutex_init’ discards ‘volatile’ qualifier from pointer target type` Can you explain for me why, please ? Thanks :) – hqt Apr 13 '14 at 09:58
  • The use of volatile in any multi-threaded C program is pretty much guaranteed to be a serious bug. That's not what volatile was intended for and is not what it does. Best case it will just hide a bug. For example the code in "How to use C's volatile keyword" does not guarantee that `task1` will ever terminate. – Voo Apr 13 '14 at 12:44
  • 1
    @hqt, there are only a few circumstances where using `volatile` makes sense and passing a volatile as a function argument is not one of those. The main use of `volatile` is to inform the compiler that some global memory location is being modified by something else so use the memory location directly rather than caching the value. Mostly I have seen it used in embedded programs to read from some memory location that is used by something else especially with a polling type of loop or something similar. It sounds to me like the compiler is warning you of an improper use of `volatile`. – Richard Chambers Apr 14 '14 at 16:48