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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:01:35+00:00 2026-06-12T14:01:35+00:00

I have written a C program that uses two threads for reading and writing.

  • 0

I have written a C program that uses two threads for reading and writing. I have declared the variable which are accessed by both the threads as Global. How to avoid the use of global variables in this case.

  • 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-12T14:01:37+00:00Added an answer on June 12, 2026 at 2:01 pm

    HOw to avoid the use of global variables in this case.

    There is no need to avoid global variables. Only thing you have to consider is valid data by some lock mechanism.

    Putting all global variables in to a struct is for readability and code control when your project grows.

    I suggest you to use mutex lock.. Here is an modified sample code.

    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef struct 
     {
       double      *a;
       double      *b;
       double     sum; 
       int     veclen; 
     } DOTDATA;
    
    /* Define globally accessible variables and a mutex */
    
    #define NUMTHRDS 4
    #define VECLEN 100
       DOTDATA dotstr; 
       pthread_t callThd[NUMTHRDS];
       pthread_mutex_t mutexsum;
    
    
    void *dotprod(void *arg)
    {
    
       /* Define and use local variables for convenience */
    
       int i, start, end, len ;
       long offset;
       double mysum, *x, *y;
       offset = (long)arg;
    
       len = dotstr.veclen;
       start = offset*len;
       end   = start + len;
       x = dotstr.a;
       y = dotstr.b;
    
       /*
       Perform the dot product and assign result
       to the appropriate variable in the structure. 
       */
    
       mysum = 0;
       for (i=start; i<end ; i++) 
        {
          mysum += (x[i] * y[i]);
        }
    
       /*
       Lock a mutex prior to updating the value in the shared
       structure, and unlock it upon updating.
       */
       pthread_mutex_lock (&mutexsum);
       dotstr.sum += mysum;
       pthread_mutex_unlock (&mutexsum);
    
       pthread_exit((void*) 0);
    }
    
    /* 
    The main program creates threads which do all the work and then 
    print out result upon completion. Before creating the threads,
    the input data is created. Since all threads update a shared structure, 
    we need a mutex for mutual exclusion. The main thread needs to wait for
    all threads to complete, it waits for each one of the threads. We specify
    a thread attribute value that allow the main thread to join with the
    threads it creates. Note also that we free up handles when they are
    no longer needed.
    */
    
    int main (int argc, char *argv[])
    {
       long i;
       double *a, *b;
       void *status;      
    
       /* Assign storage and initialize values */
       a = (double*) malloc (NUMTHRDS*VECLEN*sizeof(double));
       b = (double*) malloc (NUMTHRDS*VECLEN*sizeof(double));
    
       for (i=0; i<VECLEN*NUMTHRDS; i++)
        {
         a[i]=1.0;
         b[i]=a[i];
        }
    
       dotstr.veclen = VECLEN; 
       dotstr.a = a; 
       dotstr.b = b; 
       dotstr.sum=0;
    
       pthread_mutex_init(&mutexsum, NULL);             
    
       for(i=0; i<NUMTHRDS; i++)
       {
          /* 
          Each thread works on a different set of data.
          The offset is specified by 'i'. The size of
          the data for each thread is indicated by VECLEN.
          */
          pthread_create(&callThd[i], NULL, dotprod, (void *)i);
       }  
    
       /* Wait on the other threads */
       for(i=0; i<NUMTHRDS; i++)
       {
          pthread_join(callThd[i], &status);
       }
    
       /* After joining, print out the results and cleanup */
       printf ("Sum =  %f \n", dotstr.sum);
       free (a);
       free (b);
       pthread_mutex_destroy(&mutexsum);
       pthread_exit(NULL);
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program written in VB.NET which stops a service that uses file
I have a program written in Java which uses 3 threads. Everything is working
I have written a program that uses qhttp to get a webpage. This works
I have a program written in VB.Net (Visual Studio 2008) that uses a DLL
I have written a Linux system wide C++ program /usr/bin/PROG_X that uses a configuration
I have written down the following program that uses the quicksort algorithm to sort
Question: I have written a console program that uses the SQL server 2005 web
I have written a program that runs on Linux and uses sigevent and timer_create
I have written a very small C# program, that uses a very small SQL
I have a program written in C++ which uses dlopen to load a dynamic

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.