Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 358293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:17:09+00:00 2026-05-12T12:17:09+00:00

In C, declaring a variable static in the global scope makes it a global

  • 0

In C, declaring a variable static in the global scope makes it a global variable. Is this global variable shared among threads or is it allocated per thread?

Update:
If they are shared among threads, what is an easy way to make globals in a preexisting library unique to a thread/non-shared?

Update2:
Basically, I need to use a preexisting C library with globals in a thread-safe manner.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T12:17:09+00:00Added an answer on May 12, 2026 at 12:17 pm

    It’s visible to the entire process, i.e., all threads. Of course, this is in practice. In theory, you couldn’t say because threads have nothing to do with the C standard (at least up to c99, which is the standard that was in force when this question was asked).

    But all thread libraries I’ve ever used would have globals accessible to all threads.


    Update 1:

    Many thread libraries (pthreads, for one) will allow you to create thread-specific data, a means for functions to create and use data specific to the thread without having it passed down through the function.

    So, for example, a function to return pseudo random numbers may want each thread to have an independent seed. So each time it’s called it either creates or attaches to a thread-specific block holding that seed (using some sort of key).

    This allows the functions to maintain the same signature as the non-threaded ones (important if they’re ISO C functions for example) since the other solution involves adding a thread-specific pointer to the function call itself.

    Another possibility is to have an array of globals of which each thread gets one, such as:

    int fDone[10];
    int idx;
    : : :
    for (i = 0; i < 10; i++) {
        idx = i;
        startThread (function, i);
        while (idx >= 0)
            yield();
    }
    
    void function () {
        int myIdx = idx;
        idx = -1;
        while (1) {
            : : :
        }
    }
    

    This would allow the thread function to be told which global variable in the array belongs to it.

    There are other methods, no doubt, but short of knowing your target environment, there’s not much point in discussing them.


    Update 2:

    The easiest way to use a non-thread-safe library in a threaded environment is to provide wrapper calls with mutex protection.

    For example, say your library has a non-thread-safe doThis() function. What you do is provide a wrapper for it:

    void myDoThis (a, b) {
        static mutex_t serialize;
        mutex_claim (&serialize);
        doThis (a, b);
        mutex_release (&serialize);
    }
    

    What will happen there is that only one thread at a time will be able to claim the mutex (and hence call the non-thread-safe function). Others will be blocked until the current one returns.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 202k
  • Answers 202k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer One of the clearest explanations of the disadvantages of session… May 12, 2026 at 8:26 pm
  • Editorial Team
    Editorial Team added an answer WebRequest request = HttpWebRequest.Create("http://www.foo.com/"); request.Method = "HEAD"; // Just get… May 12, 2026 at 8:26 pm
  • Editorial Team
    Editorial Team added an answer It's easy: var variable_regex = "bar"; var some_string = "foobar";… May 12, 2026 at 8:26 pm

Related Questions

Note this question was originally posted in 2009, before C++11 was ratified and before
Garbage collection in .NET leads many to believe that lightweight objects can be treated
I'm coming from a C# background and I really like the type inference that
c#/oop noob here. I am creating a datatable from the schema of a sql

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.