7

I want know how to pass data between threads using C language.

For example:X waits for a message from somewhere.
Y sends T-X a message about an event and waits for a response.
T-X deals with the event and sends a response to T-Y.
T-X waits for another message.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Rizvan
  • 2,335
  • 2
  • 23
  • 42
  • 1
    Theads ? pthreads ? then you just have to use the same variable... static for example. threads share the same memory space – Kek Dec 06 '12 at 10:09
  • global variables with some lock mechanism??? – Jeyaram Dec 06 '12 at 10:14
  • sounds like pipes or sockets are what you are looking for. check `pipe(2)` and `socketpair(3)`. Alternatively if you are looking for a more efficient, but less synchronized user-space way then check out virtual ring buffers `vrb(3)`. – Sergey L. Dec 06 '12 at 12:12

2 Answers2

12

a sample program taken from https://computing.llnl.gov/tutorials/pthreads/#Mutexes and modified. This shows, how to use a globally declared data in multiple threads.

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

/*   
The following structure contains the necessary information  
to allow the function "dotprod" to access its input data and 
place its output into the structure.  
*/

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; //GLOBAL DATA which is going to be accessed by different threads

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);
}

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);
}    

   printf ("Sum =  %f \n", dotstr.sum);
   free (a);
   free (b);
   pthread_mutex_destroy(&mutexsum);
   pthread_exit(NULL);//No need of pthread_join() if pthread_exit() used.
}   
Jeyaram
  • 9,158
  • 7
  • 41
  • 63
0

Threads are sharing the same memory space so you can use ordinary variable to share data between threads.

You are also mentioning about thread waiting for some event, this is another story - synchronization. For this purpose you can use mutexses for example.

codewarrior
  • 1,269
  • 1
  • 9
  • 14