do I really have to do pthread_mutex_lock?

This is a discussion on do I really have to do pthread_mutex_lock? within the C Programming forums, part of the General Programming Boards category; The code : Code: #include <stdio.h> #include <stdlib.h> #include <time.h> #include <pthread.h> #define NUM_THREADS 8 typedef struct { int id; ...

  1. #1
    cph
    cph is offline
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    82

    do I really have to do pthread_mutex_lock?

    The code :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <pthread.h>
    
    #define NUM_THREADS	8
    
    typedef struct {
        int             id;
        int             *ptr_arr;
        pthread_t       thread;
        pthread_mutex_t mutex;
    } some_struct;
    
    void *some_func (void *arg)
    {
        some_struct *w = (some_struct *)arg;
    
        /* do I really need to lock? */
        pthread_mutex_lock(&w->mutex);
        w->ptr_arr[w->id] = w->id * 4;
        pthread_mutex_unlock(&w->mutex);
    }
    
    int main (void)
    {
        int         arr[NUM_THREADS];
        int         cnt;
        some_struct ss[NUM_THREADS];
    
        /* initialize */
        for (cnt = 0; cnt < NUM_THREADS; ++cnt) {
    	arr[cnt]        = cnt;
    	ss[cnt].id      = cnt;
    	ss[cnt].ptr_arr = arr;
    	pthread_mutex_init(&ss[cnt].mutex, NULL);
        }
    
        /* print array */
        for (cnt = 0; cnt < NUM_THREADS; ++cnt) {
    	printf("%3d%8d\n", cnt, arr[cnt]);
        }
    
        /* do something */
        for (cnt = 0; cnt < NUM_THREADS; ++cnt) {
    	pthread_create(&ss[cnt].thread, NULL, some_func, &ss[cnt]);
        }
    
        /* join the threads */
        for (cnt = 0; cnt < NUM_THREADS; ++cnt) {
    	pthread_join(ss[cnt].thread, NULL);
        }
    
        printf("\n");
        /* print them again */
        for (cnt = 0; cnt < NUM_THREADS; ++cnt) {
    	printf("%3d%8d\n", cnt, arr[cnt]);
        }
    
        return(0);
    }
    The explanation:
    As you can see, there are 8 threads. Each of them is modifying a single area of an array according to the thread id.

    The question:
    Do I really need to lock the mutex?
    Last edited by cph; 12-17-2008 at 07:25 PM.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by cph View Post
    The explanation:
    As you can see, there are 8 threads. Each of them is modifying a single area of an array according to the thread id.

    The question:
    Do I really need to lock the mutex?
    In theory, yes.

    Imagine that a pointer is 32 bits wide, but the platform (i.e. CPU) enforces 64-bit aligned memory reads and writes. Two array elements will fit into the same atomic memory unit, meaning that the compiler will generate code that loads/stores the same memory region for both of these accesses. They have to be synchronized or they may interfere with each other.

    On a platform-by-platform basis you may know that this is not the case. But in general, you don't.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,671
    No, you don't since there are no 2 threads in which: 1) one is reading while the other could be writing to the same memory location 2) one is writing while the other could be writing to the same memory location. Two threads can read from the same memory location without synchronization provided that the memory was initialized/set prior to thread creation.

    However, having multiple threads accessing the same array (or any adjacent memory locations) can be dangerous due to "word tearing". [edit]Which is what brewbuck is referring to. But since there is a mutex per thread, it doesn't help with potential word tearing.[/edit]

    gg

  4. #4
    cph
    cph is offline
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    82
    alright, thank you very much.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21