0
votes

I was playing around with threads using pthread_create and mutexes, but I noticed that in my simple example an int value that I store in a struct passed into pthread_create doesn't retain its value when the thread executes method SimpleThread. Below is the code. Specifically, in line 63 I assigned the count on the loop to the int id in the struct thread that is used as a parameter in pthread_create. In line 22 where I print out the value of id from the struct it always gives the same value. If I create 2 threads, the value is 2. If i create 3 threads, the value is 3. The pattern continues. I guess I'm just curious why this happens instead of getting the actual value of i like I intended in line 63.

1    #include <stdio.h>
2    #include <stdlib.h>
3    #include <pthread.h>  
4    #include <unistd.h>
5
6    #define PROGRAM_NAME 0
7    #define NUM_THREADS 1
8
9    int SharedVariable;
10   pthread_mutex_t mutex;
11
12   struct thread
13   {
14     pthread_t t;
15     int id;
16   };
17
18   void* SimpleThread( void* arg )
19   {
20     struct thread* parameter = ( struct thread* ) arg;
21     int which = parameter->id;
22     printf( "Threads IDs in SimpleThread -- %d\n", parameter->id );
23
24     pthread_mutex_lock( &mutex );
25
26     int num, val;
27
28     for( num = 0; num < 20; num++ )
29     {
30       if( random( ) > RAND_MAX / 2 )
31         usleep( 10 );
32
33       val = SharedVariable;
34
35       //printf( "***thread %d sees value %d\n", which, val );
36       SharedVariable = val + 1;
37     }
38
39     val = SharedVariable;
40     //printf( "Thread %d sees final value %d\n", which, val );
41
42     pthread_mutex_unlock( &mutex );
43
44     return ( void * ) arg; 
45   }
46
47   int main( int argc, char* argv[ ] )
48   {
49     int num_threads, i;
50
51     if( argc != 2 )
52     {
53       fprintf( stderr, "Usage: %s <num_threads>\n", argv[ PROGRAM_NAME ] );
54       return -1;
55     }
56     num_threads = atoi( argv[ NUM_THREADS ] );
57     struct thread* container = ( struct thread* ) malloc( num_threads * sizeof( struct thread ) );
58
59     pthread_mutex_init( &mutex, NULL );
60
61     for( i = 0; i < num_threads; i++ )
62     {
63       container[ i ].id = i;
64       pthread_create( &container[ i ].t, 0, SimpleThread, &container );
65     }
66
67     for( i = 0; i < num_threads; i++ )
68     {
69       pthread_join( container[ i ].t, 0 );
70     }
71
72     pthread_mutex_destroy( &mutex );
73
74     return 0;
75   }
2
struct thread* parameter = ( struct thread* ) arg;, then struct thread* container = ( struct thread* ) malloc - AAAAAAAAARRRRRGGGGHHHH! - user529758
Dont cast malloc also. - Varvarigos Emmanouil
I tried to remove the malloc but I got a segmentation fault after the output. It's my understanding that the malloc in the main function initializes the struct and then I just use the initialized struct as a parameter in the SimpleThread function. - wmarquez

2 Answers

3
votes

You're not passing the correct array element into your thread.

Instead of

pthread_create( &container[ i ].t, 0, SimpleThread, &container )

You need to do this

pthread_create( &container[ i ].t, 0, SimpleThread, &container[i] )
1
votes

You give as argument the same address to 2 different threads. You make it a shared resource in this way.