1

I am developing file system on FUSE in C. FUSE is event driven system so it matches own opcode to user's callback function. As I know static function in C means only scope limitation - i.e. if static function f() was written in foo.c, its symbol's scope is only foo.c not each other source file. But my team member said static function is not thread safe, so we should make callback non static.(our system must works in multithreading environment, so it should consider about concurrency.)

I have googled all day long but I could not find it. I want to ask your evidance or opinion about thread safty of static function in C.

Jafffy
  • 115
  • 8
  • 5
    If you mean the `static` keyword, then no, adding it will not make a function thread-safe. Making a function `static` only changes the linkage visibility of the function. Static functions and thread-safety are completely orthogonal concepts. – Some programmer dude Aug 17 '15 at 10:38
  • 4
    `static` function and thread-safeness are totally independent. – Yu Hao Aug 17 '15 at 10:38
  • You could make the case that a function which uses *static data* is usually not *reentrant*. – Blagovest Buyukliev Aug 17 '15 at 10:40
  • `static` has a total different meaning in C than in OO. It only means that the function is not visible outside the module (to other modules). – abort Aug 17 '15 at 10:41

3 Answers3

5

The static keyword defines the linkage to be internal. I.e. it only defines whether the function is visible in other compilation units when linking. It's unrelated to whether the function is thread safe or not.

Community
  • 1
  • 1
Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
4

Declaring a function static does not change it's behavior regarding multithreading. The function itself still is thread safe, as long as it does not contain any self-modfying code.

You colleague probably confused static functions with variables of static storage duration: local variables declared static. Those could indeed affect thread safety.

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
  • 'The function itself is still thread safe' sounds like a bold claim. It might modify data (e.g. global variables or variables which are accessible via pointers passed as function arguments) without any notion of synchronizing accesses. – Frerich Raabe Aug 17 '15 at 10:45
  • @FrerichRaabe That's right, the way I put it might be misleading. What I wanted to express is that adding the `static` modifier does not affect the rendered code and the way how its called in a way that reduces previously existing thread safety. Thanks for making this clear. – Nikolai Ruhe Aug 17 '15 at 12:10
0

As a few others have already noted, static does nothing with regards to thread safety. I suggest you look into mutexes if you want to avoid data races or other multithreading-related problems.

Nick Mertin
  • 1,149
  • 12
  • 27