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 8258951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:43:28+00:00 2026-06-08T02:43:28+00:00

I am trying to write a program where I have to call some functions

  • 0

I am trying to write a program where I have to call some functions through a (shared) library (its source is available). The C code for the library has several global variables, and many functions change the values of these global variables. What I have to do in my program requires that each function call that I make gets to work with a fresh set of variables.

For example, let this function be a part of the library:

int x = 1;

int foo()  
{  
    int a = 0;
    //do somethings to 'a'
    //...
    x++;
    return a;
}  

Now every time I invoke foo() from my program, the value of x gets update from 1 to 2 then 3 then 4 and so on… I am try to construct a program so that every time foo() is invoked, it sees x = 1.

I am sorry to say that my knowledge of how C/linux treat these variable spaces is insufficient, so this question may seem vague. The above is just a small example; in reality, there are so many variables that is practically impossible to reset their values manually.

What may be the best way to compile that library and/or use it my program so as to refresh the variables?

(On a side note, what I am also trying to do is to parallelize calls to foo(), but because of the shared variables, I cannot do that.)

EDIT:
When working on some web dev projects, I used to encapsulate some code in webservices and then invoke those services from the main program. Does a similar framework exist in C/Linux? Please note that functions are returning data.

  • 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-06-08T02:43:30+00:00Added an answer on June 8, 2026 at 2:43 am

    You have discovered one of the main reasons that global variables (or global state in general) are a really bad idea.

    Since you have access to the source, I would suggest investing some time to refactor the source code.

    You can achieve the ability to parallelize calls to foo with the following strategy:

    • Gather up all of the global variables into a single struct. Call it something like Context.
    • Change each function that acts on a global variable to take a pointer to a Context, and change the function to update the variables in the Context instead of updating global variables.
    • Now each thread that wants to use the library can create a new Context and pass that into foo and related functions.

    If it’s not feasible to make such a change to the source code, you can use more than one CPU core by starting child processes. Each child process has it’s own memory space. That option is not nearly as efficient as using multiple threads.

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

Sidebar

Related Questions

I'm trying to write some functions/subroutines in a module that call another function in
i have been trying to write a program in python to read two text
I'm trying to write a simple stock check program, and I have a Table
I have a C program like the one below. I am trying to write
I am trying to write an OpenGL visualization program for some scientific data using
I am trying to write a program in Java using ExecutorService and its function
I am trying to write a scientific graph library, it works but I have
I'm trying to use the C++ Boost library to write some depth data I
I have to write a program that will go through a given folder and
I have a C++ program that writes to stdout that I am trying to

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.