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

  • Home
  • SEARCH
  • 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 6175567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:56:50+00:00 2026-05-23T23:56:50+00:00

If I define a thread function that reuses another function that the main thread

  • 0

If I define a thread function that reuses another function that the main thread also uses….is it possible that there can be a race condition? Are the local variables in the same function shared across threads? In this case the function do_work is used in both the thread_one thread and the main thread. Can the local variable x in the function do_work be modified by both threads so it creates an unexpected result?

void *thread_one() {
   int x = 0;
   int result;
   while(1) {
       for(x=0; x<10; x++) {
           result = do_work(x);
       }
       printf("THREAD: result: %i\n", result);
   }
}

int do_work(int x) {
    x = x + 5;
    return x;
}

int main(int argc, char**argv) {
    pthread_t the_thread;
    if( (rc1 = pthread_create( &the_thread, NULL, thread_one, NULL)) ) {
        printf("failed to create thread %i\n", rc1);
        exit(1);
    }
    int i = 0;
    int result = 0;
    while(1) {
        for(i=0; i<12; i+=2) {
            result = do_work(i);
        }
        printf("MAIN: result %i\n", result);
    }
    return 0;
}    
  • 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-23T23:56:51+00:00Added an answer on May 23, 2026 at 11:56 pm

    No. Local variables aren’t shared across threads.

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

Sidebar

Related Questions

C++0x thread library or Boost.thread define non-member variadic template function that lock all lock
C++0x thread library or Boost.thread define a non-member variadic template function that locks all
I want to define an array outside main function so that it is shared
How to define local static variables (that keeps its value between function calls) that
Consider the following function that implements non-blocking access to only the one thread. public
I'm maintaining a library that has a function that needs thread specific variables. Due
Can someone help me to complete my code with a function that can check
Using File::Find , how can I pass parameters to the function that processes each
Are the vectors defined in the C++ STL re-entrant or thread safe? Can I
My application uses several threads with well-defined names (i.e. not a thread pool with

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.