PDA

View Full Version : سوال: ()Error 16 on : pthread_mutex_destroy



Farnoushzn
جمعه 23 فروردین 1392, 00:37 صبح
I have written a simple example on pthread_mutexattr_setprotocol. I have two problems; Although I added #define _GNU_SOURCE at top of my source code, the program did not compile in c because it could not able to identify protocol's attribute like >PTHREAD_PRIO_INHERIT and ...,in this case I had to convert it in C++‎‎‎ but know there is another problem. I can not destroy mutex. the number of error is 16 that means is being >used by another mutex but I do not which one? I tried even to unlock the mutex before to >destroy it but there was another error (22) which means that this mutex did not lock it. >can somebody tell me what should I do? I wrote my sample in eclips on ubuntu/linaro 4.6.3 Thank yoU

//#define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include "check.h"

int ShowProtocol(pthread_mutexattr_t *mta)
{
int rc;
int protocol;

printf("Check Protocol attribute\n");
rc = pthread_mutexattr_getprotocol(mta, &protocol);
checkResults("pthread_mutexattr_getprotocol()\n", rc);

printf ("%d\n" ,protocol);

printf("The Protocol attributed is: ");

switch (protocol) {
case PTHREAD_PRIO_INHERIT:
printf("PTHREAD_PRIO_INHERIT\n");
break;
case PTHREAD_PRIO_NONE:
printf("PTHREAD_PRIO_NONE\n");
break;
case PTHREAD_PRIO_PROTECT :
printf("PTHREAD_PRIO_PROTECT\n");
break;
default :
printf("! protocol Error Protocol=%d !\n", protocol);
exit(1);
}
return protocol;
}

int main(int argc, char **argv)

{
int rc=0;
pthread_mutexattr_t mta;
pthread_mutex_t mutex;


printf("Enter Testcase - %s\n", argv[0]);

printf("Create a default mutex attribute\n");
rc = pthread_mutexattr_init(&mta);
checkResults("pthread_mutexattr_init()\n", rc);

ShowProtocol(&mta);

printf("Change mutex Protocol attribute to PTHREAD_PRIO_PROTECT\n");
rc = pthread_mutexattr_setprotocol(&mta, PTHREAD_PRIO_PROTECT);
checkResults("pthread_mutexattr_setProtocol()\n", rc);
ShowProtocol(&mta);

printf("Cleanup\n");
rc = pthread_mutex_destroy(&mutex);
checkResults("pthread_mutex_destroy()\n", rc);

rc = pthread_mutexattr_destroy(&mta);
checkResults("pthread_mutexattr_destroy()\n", rc);

printf("Main completed\n");
return 0;
}


#ifndef CHECK_H_
#define CHECK_H_

/* headers used by a majority of the example program */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

/* Simple function to check the return code and exit the program
if the function call failed
*/
static void checkResults(char *string, int rc) {
if (rc) {
printf("Error on : %s, rc=%d",
string, rc);
exit(EXIT_FAILURE);
}
return;
}

#endif /* CHECK_H_ */

FastCode
جمعه 23 فروردین 1392, 01:00 صبح
First I suggest you use the C++‎‎‎ tag in the editor to make your code aligned correctly and readable.

Second, There are tons of documentation about pthread on the Internet and everything else everyone babbles when you should have googled first.

you need to free/unlock the mutex before destroying it.because the mutex won't be able to release an unknown number of waiting threads at once.Its a mutex its not a geek.
pthread_mutex_unlock();

http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_mutex_init.html

Farnoushzn
جمعه 23 فروردین 1392, 12:07 عصر
First I suggest you use the C++‎‎‎‎ tag in the editor to make your code aligned correctly and readable.

Second, There are tons of documentation about pthread on the Internet and everything else everyone babbles when you should have googled first.

you need to free/unlock the mutex before destroying it.because the mutex won't be able to release an unknown number of waiting threads at once.Its a mutex its not a geek.
pthread_mutex_unlock();

http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_mutex_init.html


Thank you for replying me. But If you paid attention to what I have written, I said: the program have syntax error in C on PTHREAD_PRIO_INHERIT and does not know it as mutex protocol in pthread.
I found in Internet that I should add “ #define _GNU_SOURCE “at top of my source code but did not solved,So I converted my program in c++ . In this case I have another problem to destroy it. I said that even I unlock the mutex it give the error 22 that shows I unlock a mutex that did not lock the program,whereas I have only one mutex. I dont know what is the problem