CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2003
    Location
    San Antonio TX
    Posts
    380

    mutex and pthread

    Platform: AIX

    I am new to using mutex'es and pthreads so I have been writing some very simple test programs to just understand the concept and the syntax.

    Well, my first non-threaded app seemed to work just fine with mutex'es (the function calls did not return any errors), but now that I have introduced pthreads and some of the mutex functions are now returning errors.

    Here is the code:
    Code:
    #include <pthread.h>
    #include <stdio.h>
    
    //GLOBAL MUTEX
    pthread_mutex_t gMutex;
    
    void*  mutex_test(void*);
    
    int main(int argc, char** argv)
    {
        int rv_init    = 0;
        int rv_destroy = 0;
        int rv_thread  = 0;
    
        int i = 0;
    
        pthread_t threads[10];
    
        char thread_ids[10][2] ={"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
    
        //INITIALIZE MUTEX
        rv_init = pthread_mutex_init(&gMutex, 0);
    
        printf("\nMutex Initialization Status: %i", rv_init);
    
        //LAUNCHING THREADS
        for(i = 0; i < 2; i++)
        {
            rv_thread = pthread_create(&threads[i], 0, mutex_test, (void*)(&thread_ids[i]));
        }
    
    
        //DESTROY MUTEX
        rv_destroy = pthread_mutex_destroy(&gMutex);
    
        printf("\nMutex Destruction Status: %i", rv_destroy);
    
        pthread_exit(0);
    
        return 0;
    }
    
    void* mutex_test(void* pszThreadId)
    {
        char* pszId = (char*)pszThreadId;
    
        unsigned int i = 0;
        int rv_lock   =  0;
        int rv_unlock =  0;
        time_t start_t = 0;
        time_t now_t   = 0;
    
        for(i = 0; i < 4; i++)
        {
            printf("\n[%s]: Starting...", pszId);
            printf("\n[%s]: Attempting to lock mutex...", pszId);
    
            rv_lock = pthread_mutex_lock(&gMutex);
    
            printf("\n[%s]: mutex locked - rv_lock == %i", pszId, rv_lock);
            printf("\n[%s]: holding for a sec ...", pszId);
    
            time(&start_t);
    
            while((start_t + 3) > now_t)
                time(&now_t);
    
            printf("\n[%s]: unlocking the mutex now", pszId);
    
            rv_unlock = pthread_mutex_unlock(&gMutex);
    
            printf("\n[%s]: mutex has been unlocked - rv_unlock == %i", pszId, rv_unlock);
        }
    
    
    }
    The functions in main both return 0 - success:
    pthread_mutex_init()
    pthread_mutex_destroy()

    The function in the thread (mutex_test) both return 22
    pthread_mutex_lock()
    pthread_mutex_unlock()

    My first, non-threaded, attempt just had all the code in main(), now I have moved the mutex (gMutex) to being a global variable and it is being initialized in main, but used in the thread function (mutex_test)

    Any help would be appreciated.

    Thanks
    John 3:16
    For God so loved the world ...

  2. #2
    Join Date
    May 2003
    Location
    San Antonio TX
    Posts
    380

    Re: mutex and pthread

    Sorry, I noticed my problem right when I posted the question.

    I was destroying the mutex before the threads were done.
    John 3:16
    For God so loved the world ...

  3. #3
    Join Date
    Dec 2002
    Location
    Kiev, Ukraine
    Posts
    61

    Re: mutex and pthread

    you can call pthread_join(...) to wait for the threads to finish before destroying the mutex. (http://www.opengroup.org/onlinepubs/...read_join.html)

  4. #4
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: mutex and pthread

    [ Moved ]
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured