about summary refs log tree commit diff stats
path: root/tests/test11.c
blob: 7d790875b62f3ad6a6c2ee2e4d93b6dc7c9e66d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#define _MULTI_THREADED
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>

void foo(void);  /* Functions that use the TLS data */
void bar(void);

#define checkResults(string, val) {             \
 if (val) {                                     \
   printf("Failed with %d at %s", val, string); \
   exit(1);                                     \
 }                                              \
}
 
/* 
   Use the keyword provided by pthread.h to delcare the following variable
   is thread specific, i.e. it is only visible to a specific thread, 
   not shared/common to all thread.
   These variables are stored in thread local storage (TLS) area.
 */
__thread int TLS_data1 = 10;
__thread int TLS_data2 = 20;
__thread char TLS_data3[10];
 
#define  NUMTHREADS   2 
pthread_t             thread[NUMTHREADS];

typedef struct {
   int   data1;
   int   data2;
} threadparm_t; 

void *thread_run(void *parm)
{
   int               rc;
   threadparm_t     *gData;

   printf("Thread %d: Entered (%d/%d)\n", (pthread_self()==thread[0])?1:2, TLS_data1, TLS_data2);

   gData = (threadparm_t *)parm;

   /* Assign the value from global variable to thread specific variable*/
   TLS_data1 = gData->data1;
   TLS_data2 = gData->data2;
   strcpy(TLS_data3, "---");
   TLS_data3[1] = (pthread_self()==thread[0])?'1':'2';

   foo();
   return NULL;
}
 
void foo() {
   printf("Thread %d: foo(), TLS data=%d %d \"%s\"\n",
          (pthread_self()==thread[0])?1:2, TLS_data1, TLS_data2, TLS_data3);
   while(!thread[1])
      usleep(300);
   if(pthread_self()==thread[0])
      pthread_join(thread[1], NULL);
   bar();
}
 
void bar() {
   printf("Thread %d: bar(), TLS data=%d %d \"%s\"\n",
          (pthread_self()==thread[0])?1:2, TLS_data1, TLS_data2, TLS_data3);
   return;
}
 

int main(int argc, char **argv)
{
  int                   rc=0;
  int                   i;
  threadparm_t          gData[NUMTHREADS];
 
  printf("Create/start %d threads\n", NUMTHREADS);
  for (i=0; i < NUMTHREADS; i++) { 
     /* Create per-thread TLS data and pass it to the thread */
     gData[i].data1 = i;
     gData[i].data2 = (i+1)*2;
     rc = pthread_create(&thread[i], NULL, thread_run, &gData[i]);
     checkResults("pthread_create()\n", rc);
     usleep(200);
  }
 
  //printf("Wait for all threads to complete, and release their resources\n");
  for (i=0; i < NUMTHREADS; i++) {
     rc = pthread_join(thread[i], NULL);
     //checkResults("pthread_join()\n", rc);
  }

  printf("Main completed\n");
  return 0;
}