Thread: Communication/Synchronization between threads

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147

    Communication/Synchronization between threads

    Iam trying to understand and find solution to one of my problem .

    Let me explain my logic and the blocking aspect in that.

    There are two threads

    Thread1 and Thread2 these two operate on globa buffer.

    Thread1 generates global list gl , thread 2 uses it by copying it into its own local copy .


    The global buffer is secured by mutex lock.

    Here thread2 is in infinite loop like below

    Code:
    while(1)
    {
    pthread_mutex(&lock1);
    
    pthread_mutex_lock(&lock2);
    global list copy to local list.
    pthread_mutex_unlock(&lock2);
    
    for(i=0;i<number of nodes in the list;i++)
    {
    process the local list
    }
    
    }
    the thread2 would be waken up by thread1 by unlocking the mutex lock1.

    like below
    Code:
    thread1
    {
    pthread_mutex_lock(&lock2);
    prepare global list gl 
    pthread_mutex_unlock(&lock2)
    
    ptherad_mutex_unlock(&lock1)
    }
    The blocking issue for me here is ,
    lets take a case where thread2 is in processing the list ,
    in the mean time if thread1 provides new global list ,

    How will thread 2 behave,i am afraid that this will corrupt the local list ( as this is not secured by lock ) and also there is no mechanism to tell the thread 2 from thread 1 that to consider the new list .

    How can i over come this problem .

    You help would be highly appreciated.

  2. #2
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    Hi,

    I think I understand what you want to do, but you cannot unlock a mutex in another thread than in the one you locked it. You should take a look at the condition variables (pthread_cond_xxx()).

    Hope this helps.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Here is a simple producer-consumer example using a single variable as the shared data.
    Mutex, Cond and Thread questions (pthread, linux)

    gg

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by vlrk View Post
    The blocking issue for me here is ,
    lets take a case where thread2 is in processing the list ,
    in the mean time if thread1 provides new global list ,

    How will thread 2 behave,i am afraid that this will corrupt the local list ( as this is not secured by lock )
    How? It would appear the local list is not accessed by any other thread, so it does not need to be locked, nor could it be corrupted by anything while thread 2 is working with it.

    and also there is no mechanism to tell the thread 2 from thread 1 that to consider the new list .
    Won't that happen by design at the next iteration of the loop?

    How can i over come this problem .
    Your problem is more related to Tibo-88's comment; unlocking a mutex locked by another thread results in undefined behaviour. Ie, your idea may appear to be working, but it is not valid.

    You can get out of that one by using the locks differently, but it is not worth it, because there will still be nothing to guarantee the alternation of the two threads. One thread could end up repeatedly acquiring the lock. You could have some common state variable to check inside the locked section so that if there is nothing to do, thread x immediately releases the lock, but again, there is no guarantee it won't just keep doing this endlessly (probably not forever, but enough that you will be wasting substantial processor power in what is essentially a "busy loop"). Not even introducing passive time delays will guarantee anything (altho again, it may seem to work) -- and at the point when you feel the need for time delays to get your synchronization to work, you have gone far off course. You will have a highly inefficient, error prone, wrongly conceived multi-threaded program.

    The proper way to do this is as per Codeplug's example, by using a condition variable.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147
    Thank you all for giving this info.

    I would look into the codePlug suggestion.

    regards

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. threads and synchronization
    By Drogin in forum C Programming
    Replies: 14
    Last Post: 09-26-2010, 12:55 PM
  2. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 07:49 AM
  3. Synchronization ...
    By Deb in forum C Programming
    Replies: 5
    Last Post: 04-12-2007, 11:26 PM
  4. help with threads communication
    By BrownB in forum C++ Programming
    Replies: 3
    Last Post: 05-23-2006, 03:37 AM
  5. a point of threads to create multiple threads
    By v3dant in forum C Programming
    Replies: 3
    Last Post: 10-06-2004, 09:48 AM