Yifu Wang
Yifu Wang

Reputation: 313

Getting "define but not used" warning when using gcc "__thread" specifier

I currently have a library with some global variables. I want to make these variables thread local so I added "__thread" specifier in front of them. It does the job but the compiler gives "define but not used" warnings on these variable. I hid the warnings with "-Wno-unused-variable", but I wonder why it happens because these variables are actually being used in the library.

Upvotes: 1

Views: 810

Answers (1)

Jens Gustedt
Jens Gustedt

Reputation: 78903

If they are really declared with static, as indicated in a comment, your compiler is probably right and this is just a waste of resources, since you are creating a new thread local variable in every compilation unit.

If you want that static allocation of global variables to change to sensible use of thread local variable, you'd have to do a bit more. Use a declaration as this

extern thread_local double eps;

in your header file and a definition

thread_local double eps;

in just one of your .c files.

Note also that thread local variables now are part of the C standard (C11) and that the keyword there is _Thread_local, with a standard "abbreviation" of thread_local. If your compiler doesn't support this yet, you can easily #define this conditionally to __thread or whatever compiler extension provides you with that feature.

Upvotes: 1

Related Questions