I hope this helps:
#include <libc.h>
#include <pthread.h>
pthread_mutex_t mutex_1;
pthread_mutexattr_t attr_1;
pthread_mutex_t mutex_2;
pthread_mutexattr_t attr_2;
int main()
{
int ret;
int recursive;
//
// Test 1, non-recursive mutex
//
//
// Init Mutex Attr 1
//
ret = pthread_mutexattr_init(&attr_1);
if (ret)
{
fprintf(stderr,"Mutex Init Attr 1 failed error %d\n",ret);
exit(-1);
}
else
{
fprintf(stderr,"Mutex Init Attr 1 (non-recursive) Successful\n");
}
//
// Set Recursive value of Mutex Attr 1
//
ret = pthread_mutexattr_setrecursive(&attr_2,PTHREAD_RECURSIVE_DISABLE);
if (ret)
{
fprintf(stderr,"Mutex Attr SetRecursive 1 (non-recursive) failed error %d\n",ret);
exit(-1);
}
else
{
fprintf(stderr,"Mutex Attr SetRecursive 2 (non-recursive) Successful\n");
}
//
// Check Recursive value of Mutex Attr 1
//
ret = pthread_mutexattr_getrecursive(&attr_1,&recursive);
if (ret)
{
fprintf(stderr,"Mutex Attr GetRecursive 1 (non-recursive) failed error %d\n",ret);
exit(-1);
}
else
{
fprintf(stderr,"Mutex Attr GetRecursive 1 (non-recursive) Successful, value %d\n",recursive);
}
//
// Init Mutex 1
//
ret = pthread_mutex_init(&mutex_1,&attr_1);
if (ret)
{
fprintf(stderr,"Mutex Init 1 failed error %d\n",ret);
exit(-1);
}
else
{
fprintf(stderr,"Mutex Init 1 (non-recursive) Successful\n");
}
//
// Lock Mutex 1, first time
//
ret = pthread_mutex_lock(&mutex_1);
if (ret)
{
fprintf(stderr,"Mutex 1 Lock failed error %d\n",ret);
exit(-1);
}
else
{
fprintf(stderr,"Mutex 1 Lock Successful\n");
}
//
// Lock Mutex 1, 2nd time
//
ret = pthread_mutex_lock(&mutex_1);
if (ret)
{
fprintf(stderr,"Mutex 1 2nd Lock failed error %xh EDEADLK=%xh\n",ret,EDEADLK);
}
else
{
fprintf(stderr,"Mutex 1 2nd Lock Successful? (This would be bad)\n");
exit(-1);
}
fprintf(stderr,"\n");
//
// Test 2, recursive mutex
//
//
// Init Mutex Attr 2
//
ret = pthread_mutexattr_init(&attr_2);
if (ret)
{
fprintf(stderr,"Mutex Init Attr 2 failed error %d\n",ret);
exit(-1);
}
else
{
fprintf(stderr,"Mutex Init Attr 2 (recursive) Successful\n");
}
//
// Set Recursive value of Mutex Attr 2
//
ret = pthread_mutexattr_setrecursive(&attr_2,PTHREAD_RECURSIVE_ENABLE);
if (ret)
{
fprintf(stderr,"Mutex Attr SetRecursive 2 failed error %d\n",ret);
exit(-1);
}
else
{
fprintf(stderr,"Mutex Attr SetRecursive 2 Successful\n");
}
//
// Check Recursive value of Mutex Attr 2
//
ret = pthread_mutexattr_getrecursive(&attr_2,&recursive);
if (ret)
{
fprintf(stderr,"Mutex Attr GetRecursive 2 failed error %d\n",ret);
exit(-1);
}
else
{
fprintf(stderr,"Mutex Attr GetRecursive 2 (recursive) Successful, value %d\n",recursive);
}
//
// Init Mutex 2
//
ret = pthread_mutex_init(&mutex_2,&attr_2);
if (ret)
{
fprintf(stderr,"Mutex Init 2 failed error %d\n",ret);
exit(-1);
}
else
{
fprintf(stderr,"Mutex Init 2 (recursive) Successful\n");
}
//
// Lock Mutex 2, first time
//
ret = pthread_mutex_lock(&mutex_2);
if (ret)
{
fprintf(stderr,"Mutex 2 Lock failed error %d\n",ret);
exit(-1);
}
else
{
fprintf(stderr,"Mutex 2 Lock Successful\n");
}
//
// Lock Mutex 2, 2nd time
//
ret = pthread_mutex_lock(&mutex_2);
if (ret)
{
fprintf(stderr,"Mutex 2 2nd Lock failed error %xh\n",ret);
}
else
{
fprintf(stderr,"Mutex 2 2nd Lock Successful\n");
exit(-1);
}
}