LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-17-2010, 05:40 AM   #1
manohar
Member
 
Registered: Dec 2010
Posts: 42

Rep: Reputation: 2
Smile recusive mutex problem


Hi,

Iam suppose to use recursive mutex in my code but, iam getting following error while compiling. Iam working on linux platform. Wil somebody help on this...!!

error: ‘PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP’ undeclared here (not in a function)


 
Old 12-17-2010, 09:56 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Um... What language are you using? Can you reduce your source code to a small file which doesn't work, illustrating simply the error you're generating, and then post it?
 
Old 12-17-2010, 11:43 AM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
You're probably using C or C++...
... and you probably need to declare "PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP" as a global, "extern" variable in a .h header file. For example:

http://www.learncpp.com/cpp-tutorial...bal-variables/
 
Old 12-17-2010, 11:55 AM   #4
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, as I understand it, is a glibc nptl linux pthread #define.

You may need to use _GNU_SOURCE defined in your build or source file to have it included.
 
Old 12-17-2010, 12:45 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
orgcandman -

I thought "PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP" was probably a user-defined global variable. But you're right - it's a pthreads variable. Specifically, a pthreads enumeration:
pthread.h =>
Code:
...
enum
{
  PTHREAD_MUTEX_TIMED_NP,
  PTHREAD_MUTEX_RECURSIVE_NP,
  ...
Soooo...

manohar -

Please give us more details about what you're doing, just as Snark1994 asked. What language are you using? How are you using "PTHREAD_MUTEX_RECURSIVE_NP"? Can you provide code snippets?

Also: have you #include'ed <pthread.h>? That's kind of important if you're writing a pthreads program (at least if you're writing it in C or C++)
 
Old 12-20-2010, 12:11 AM   #6
manohar
Member
 
Registered: Dec 2010
Posts: 42

Original Poster
Rep: Reputation: 2
Hi,

Sorry for the late response...!! Iam using C program in linux platform. I have included pthread.h in the source file and again i declared "PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP" as a global variable.

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>

#define NUM_HANDLER_THREADS 3

pthread_mutex_t request_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;

pthread_cond_t got_request = PTHREAD_COND_INITIALIZER;


Instead of "PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP" this if i use "PTHREAD_MUTEX_INITIALIZER" it is compiling correctly but i need recursive mutex. I also tried defining _GNU_SOURCE in the code but there is no improvement...!!
 
Old 12-20-2010, 12:17 PM   #7
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
You say you did a #define. I'm betting you didn't do it correctly.

Code:
[01:26:48][aconole@chatham:/vobs/vendors/vendors_iq/Aricent/cpm]
$ cat foo.c 
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>

#define NUM_HANDLER_THREADS 3

pthread_mutex_t request_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;

pthread_cond_t got_request = PTHREAD_COND_INITIALIZER;

int main()
{
    pthread_mutex_lock(&request_mutex);

    printf("Locked!\n");

    pthread_mutex_unlock(&request_mutex);

    return 0;
}
[01:26:50][aconole@chatham:/vobs/vendors/vendors_iq/Aricent/cpm]
$ gcc -o foo foo.c -pthread
foo.c:7: error: ‘PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP’ undeclared here (not in a function)
Failure.

Now for success:

Code:
[01:27:20][aconole@chatham:/vobs/vendors/vendors_iq/Aricent/cpm]
$ cat foo.c 
#define _GNU_SOURCE
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>

#define NUM_HANDLER_THREADS 3

pthread_mutex_t request_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;

pthread_cond_t got_request = PTHREAD_COND_INITIALIZER;

int main()
{
    pthread_mutex_lock(&request_mutex);

    printf("Locked!\n");

    pthread_mutex_unlock(&request_mutex);

    return 0;
}
[01:27:25][aconole@chatham:/vobs/vendors/vendors_iq/Aricent/cpm]
$ gcc -o foo foo.c -pthread
[01:27:28][aconole@chatham:/vobs/vendors/vendors_iq/Aricent/cpm]
$
You can also add the #define to all of your sources by adding:
Code:
-D_GNU_SOURCE
to your compilation flags for gcc/g++.

Last edited by orgcandman; 12-20-2010 at 12:19 PM.
 
Old 12-20-2010, 10:58 PM   #8
manohar
Member
 
Registered: Dec 2010
Posts: 42

Original Poster
Rep: Reputation: 2
Smile

Hi,
Thanxs for correcting me...!! I did a mistake in regarding #define _GNU_SOURCE. I defined this after including pthread.h which is junk ...
/* wrong */
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#define _GNU_SOURCE

/* Correct */
#define _GNU_SOURCE
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
Thank you very much...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with locking pthread mutex johnbellone Programming 4 03-20-2009 12:43 PM
Mutex destroy failure problem keen4linux Linux - Software 0 04-26-2007 11:20 PM
Threads synchronisation problem (mutex variables and contitional variables) zahadumy Programming 6 12-07-2005 12:30 PM
problem installing KDE themes-"Mutex destroy failure:Devise or resource busy" kpachopoulos Linux - Newbie 2 02-28-2004 03:18 PM
problem installing KDE themes-"Mutex destroy failure:Devise or resource busy" kpachopoulos Linux - Software 0 02-28-2004 08:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration