Farnoushzn
جمعه 23 فروردین 1392, 01: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_ */
//#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_ */