Sponsored Content
Top Forums Programming using pthread_mutex_destroy() Post 302178214 by ramen_noodle on Tuesday 25th of March 2008 01:58:36 AM
Old 03-25-2008
In your case it would not make sense. It would serialize the program flow which is not what you want probably.

The reason I suggested using pthread_cond_wait (somewhat naively) is that I don't see any locking around your lock_flag variable. If that's the case and multiple threads could access the variable...races abound.

Here's an entirely contrived example for clarity. it's a useless exercise of course.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>

#define INTERVAL 5

int flag = 0;
pthread_cond_t waitvar = PTHREAD_COND_INITIALIZER;

pthread_mutex_t *mutex_create(pthread_mutexattr_t * attr)
{
    pthread_mutex_t *new_mutex;

    if ((new_mutex = malloc(sizeof(pthread_mutex_t))) == NULL) {
	return NULL;
    }
    pthread_mutexattr_init(attr);
    pthread_mutex_init(new_mutex, attr);
    return new_mutex;
}



void *create_and_wait(void *mtx)
{
    time_t now, then;
    pthread_mutex_t *amtx = mtx;

    now = then = time(NULL);
    while ((now = time(NULL)) < then + INTERVAL) {
	sleep(1);
	printf("In thread %d doing countdown.\n", pthread_self());
    }
    pthread_mutex_lock(amtx);
    flag = pthread_self();
    pthread_cond_broadcast(&waitvar);
    pthread_mutex_unlock(amtx);
    pthread_exit(NULL);
}


int main(void)
{
    int y = 0, oldflag = flag;
    pthread_t _thread;
    pthread_mutexattr_t new;
    pthread_mutex_t *tmtx;


    while ((y++) < INTERVAL) {
	if ((tmtx = mutex_create(&new)) == NULL) {
	    break;
	}			/*might as well be NULL - but new could be set to whatever */
	pthread_mutex_lock(tmtx);	/*obtain lock before blocking on mutex */
	pthread_create(&_thread, NULL, create_and_wait, tmtx);

	/* 
	 * wait for thread to signal it's wait is over via setting the flag variable to it's thread id and calling pthread_cond_signal
	 * join on the exited thread and reset oldflag
	 * unlock the temp mutex - must be done before calling pthread_mutex_destroy or EBUSY
	 * clear the mutex attributes, etc.. (whatever pthread_mutex_destroy does for the implementation)
	 * cleanup and start over
	 */

	while (flag == oldflag) {
	    pthread_cond_wait(&waitvar, tmtx);
	    pthread_join(flag, NULL);
            oldflag = flag;
	    pthread_mutex_unlock(tmtx);
	    printf("Thread id %d exited.\npthread_mutex_destroy = %d.\n",
		   flag, pthread_mutex_destroy(tmtx));
	    printf("Mutex at %p.\n", tmtx);
	    free(tmtx);
	    tmtx = NULL;
	    break;
	}
    }
    return y;
}

 
PTHREAD_MUTEX(3)					   BSD Library Functions Manual 					  PTHREAD_MUTEX(3)

NAME
pthread_mutex -- mutual exclusion primitives LIBRARY
POSIX Threads Library (libpthread, -lpthread) SYNOPSIS
#include <pthread.h> int pthread_mutex_init(pthread_mutex_t * restrict mutex, const pthread_mutexattr_t * restrict attr); int pthread_mutex_destroy(pthread_mutex_t *mutex); int pthread_mutex_lock(pthread_mutex_t *mutex); int pthread_mutex_trylock(pthread_mutex_t *mutex); int pthread_mutex_unlock(pthread_mutex_t *mutex); pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; DESCRIPTION
The pthread_mutex_init() function creates a new mutex, with attributes specified with attr. If attr is NULL, the default attributes are used. The macro PTHREAD_MUTEX_INITIALIZER can be used to initialize a mutex when the default attributes are appropriate and the mutex can be stati- cally allocated. The behavior is similar to pthread_mutex_init() with attr specified as NULL, except that no error checking is done. The pthread_mutex_destroy() function frees the resources allocated for mutex. It is possible to reinitialize a destroyed mutex, but unde- fined behavior may follow if the destroyed object is otherwise referenced. The pthread_mutex_lock() function locks mutex. If the mutex is already locked, the calling thread will block until the mutex becomes avail- able. The error conditions may vary depending on the type of the mutex; see pthread_mutexattr(3) for additional details. The pthread_mutex_trylock() function locks mutex. If the mutex is already locked, pthread_mutex_trylock() will not block waiting for the mutex, but will return an error condition. The pthread_mutex_unlock() function unlocks an acquired mutex. When operating with the default mutex type, undefined behavior follows if a thread tries to unlock a mutex that has not been locked by it, or if a thread tries to release a mutex that is already unlocked. RETURN VALUES
Upon success all described functions return zero. Otherwise, an error number will be returned to indicate the error. ERRORS
pthread_mutex_init() may fail if: [EAGAIN] The system lacks the resources to initialize another mutex. [EINVAL] The value specified by attr is invalid. [ENOMEM] The process cannot allocate enough memory to initialize another mutex. pthread_mutex_destroy() may fail if: [EBUSY] Mutex is locked by another thread. [EINVAL] The value specified by mutex is invalid. pthread_mutex_lock() may fail if: [EDEADLK] A deadlock would occur if the thread blocked waiting for mutex. [EINVAL] The value specified by mutex is invalid. pthread_mutex_trylock() may fail if: [EBUSY] Mutex is already locked. [EINVAL] The value specified by mutex is invalid. pthread_mutex_unlock() may fail if: [EINVAL] The value specified by mutex is invalid. [EPERM] The current thread does not hold a lock on mutex. SEE ALSO
pthread(3), pthread_barrier(3), pthread_cond(3), pthread_mutexattr(3), pthread_rwlock(3), pthread_spin(3) STANDARDS
These functions conform to IEEE Std 1003.1-2001 (``POSIX.1''). BSD
July 8, 2010 BSD
All times are GMT -4. The time now is 04:59 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy