Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Noncompliant Code Example

Code Block
bgColor#ccccff#ffcccc


#include <pthread.h>

void *thread1(void *ptr);
void *thread2(void *ptr);

pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER;

void *thread1(void *ptr) {

	pthread_mutex_lock(&m1);
	/* do some stuff that require locking mutex1 
	 */

	pthread_mutex_lock(&m2);
	/* do some stuff that require locking mutex2
	 */

	pthread_mutex_unlock(&m2);
	pthread_mutex_unlock(&m1);

	return NULL;
}

void *thread2(void *ptr) {

	pthread_mutex_lock(&m2);
	/* do some stuff that require locking mutex2
	 */

	pthread_mutex_lock(&m1);
	/* do some stuff that require locking mutex1
	 */

	pthread_mutex_unlock(&m1);
	pthread_mutex_unlock(&m2);

	return NULL;
}

Noncompliant Code Example

Code Block
bgColor#ccccff

#include <pthread.h>

void *thread1(void *ptr);
void *thread2(void *ptr);

pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER;

void *thread1(void *ptr) {

	pthread_mutex_lock(&m1);
	pthread_mutex_lock(&m2);

        /* do some stuff that require locking mutex1
	 */
	/* do some stuff that require locking mutex2
	 */

	pthread_mutex_unlock(&m2);
	pthread_mutex_unlock(&m1);

	return NULL;
}

void *thread2(void *ptr) {

        pthread_mutex_lock(&m1);
	pthread_mutex_lock(&m2);
	/* do some stuff that require locking mutex1
	 */
	/* do some stuff that require locking mutex2
	 */

	pthread_mutex_unlock(&m1);
	pthread_mutex_unlock(&m2);

	return NULL;
}