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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:31:46+00:00 2026-05-22T17:31:46+00:00

When using pthread, I can pass data at thread creation time. What is the

  • 0

When using pthread, I can pass data at thread creation time.

What is the proper way of passing new data to an already running thread?

I’m considering making a global variable and make my thread read from that.

Thanks

  • 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-22T17:31:47+00:00Added an answer on May 22, 2026 at 5:31 pm

    That will certainly work. Basically, threads are just lightweight processes that share the same memory space. Global variables, being in that memory space, are available to every thread.

    The trick is not with the readers so much as the writers. If you have a simple chunk of global memory, like an int, then assigning to that int will probably be safe. Bt consider something a little more complicated, like a struct. Just to be definite, let’s say we have

    struct S { int a; float b; } s1, s2;
    

    Now s1,s2 are variables of type struct S. We can initialize them

    s1 = { 42,  3.14f };
    

    and we can assign them

    s2 = s1;
    

    But when we assign them the processor isn’t guaranteed to complete the assignment to the whole struct in one step — we say it’s not atomic. So let’s now imagine two threads:

    thread 1:
       while (true){
          printf("{%d,%f}\n", s2.a, s2.b );
          sleep(1);
       }
    
    thread 2:
       while(true){
          sleep(1);
          s2 = s1;
          s1.a += 1;
          s1.b += 3.14f ;
       }
    

    We can see that we’d expect s2 to have the values {42, 3.14}, {43, 6.28}, {44, 9.42} ….

    But what we see printed might be anything like

     {42,3.14}
     {43,3.14}
     {43,6.28}
    

    or

     {43,3.14}
     {44,6.28}
    

    and so on. The problem is that thread 1 may get control and “look at” s2 at any time during that assignment.

    The moral is that while global memory is a perfectly workable way to do it, you need to take into account the possibility that your threads will cross over one another. There are several solutions to this, with the basic one being to use semaphores. A semaphore has two operations, confusingly named from Dutch as P and V.

    P simply waits until a variable is 0 and the goes on, adding 1 to the variable; V subtracts 1 from the variable. The only thing special is that they do this atomically — they can’t be interrupted.

    Now, do you code as

    thread 1:
       while (true){
          P();
          printf("{%d,%f}\n", s2.a, s2.b );
          V();
          sleep(1);
       }
    
    thread 2:
       while(true){
          sleep(1);
          P();
          s2 = s1;
          V();
          s1.a += 1;
          s1.b += 3.14f ;
       }
    

    and you’re guaranteed that you’ll never have thread 2 half-completing an assignment while thread 1 is trying to print.

    (Pthreads has semaphores, by the way.)

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

Sidebar

Related Questions

Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters
I wrote a program in C++ under Linux. For thread I am using pthread.
Using gdb, I can type 'info threads' or 'thread apply all backtrace' to get
I'm allocating my pthread thread-specific data from a fixed-size global pool that's controlled by
What free tools can I use to debug multithreaded programs created using the pthread
I tried passing a structure as the 4th argument while using pthread_create() with something
i'm working with a multi-threaded program (using pthreads) that currently create a background thread
Using online interfaces to a version control system is a nice way to have
Using PyObjC , you can use Python to write Cocoa applications for OS X.
Using VS2008, C#, .Net 2 and Winforms how can I make a regular Button

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.