LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 03-19-2009, 03:21 PM   #1
johnbellone
LQ Newbie
 
Registered: Mar 2009
Location: New Jersey
Distribution: Ubuntu, RedHat, Solaris, OS X
Posts: 9

Rep: Reputation: 0
Problem with locking pthread mutex


I am attempting to wrap calls to the C++ std::queue object with a mutex but I am running into a problem. It seems that every time the mutex is called it is crashing instead of the pthread_mutex_lock call. It seems that the mutex was not initialized but that is not the case. I am including URLs to the Mutex code so that it can be seen quickly.

Mutex.cpp
Mutex.hpp
Queue.hpp

GDB backtrace of thread that is dying
Code:
#0  0x002c83a0 in pthread_mutex_lock () from /lib/libpthread.so.0
#1  0x004b46c6 in pthread_mutex_lock () from /lib/libc.so.6
#2  0x00312dea in concurrent::IMutex::lock (this=0x68742edc) at thread/Mutex.cpp:29
#3  0x0030b50e in concurrent::Queue<proactor::Event*>::push (this=0x68742ed8, x=@0xb54d536c)
    at proactor/../thread/Queue.hpp:50
#4  0x0030b58e in proactor::Proactor::pushInputQueue (this=0x68742e78, e=0, buf=
        {static npos = 4294967295, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0xb54d63a8 "\034?\237\b"}}) at proactor/Proactor.hpp:39
#5  0x0030a841 in ConnectionThread::run (this=0x89fd198, null=0x0) at Connection.cpp:30
#6  0x00313660 in thread_run (runner=0x89fd198) at thread/Thread.cpp:7
#7  0x002c645b in start_thread () from /lib/libpthread.so.0
#8  0x004a7e5e in clone () from /lib/libc.so.6
Thanks guys this problem has really stumped me.

Last edited by johnbellone; 03-19-2009 at 03:22 PM.
 
Old 03-19-2009, 07:59 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
My guess is that because you don't have copy constructors or assignment operators for any of your classes, one instance gets copied with its mutex specs, then it destructs destroying the mutex with it, leaving the newly-copied instance with a bad mutex. If not, maybe do the following:
Code:
RecursiveMutex::RecursiveMutex (void) : mutex(), attrib() {
  pthread_mutexattr_init (&attrib);
  pthread_mutexattr_settype (&attrib, PTHREAD_MUTEX_RECURSIVE);

  pthread_mutex_init (&mutex, &attrib);
}
Also, you normally wouldn't use void when specifying function arguments in C++.
Kevin Barry
 
Old 03-20-2009, 10:22 AM   #3
johnbellone
LQ Newbie
 
Registered: Mar 2009
Location: New Jersey
Distribution: Ubuntu, RedHat, Solaris, OS X
Posts: 9

Original Poster
Rep: Reputation: 0
I ended up restructuring my Mutex classes for the better but I came across something in C++ that I never knew before. There was a NULL pointer calling the method that would push something onto this queue. So in essence the language was executing a method to a NULL object. I have a feeling this has something to do with the fact that both of the methods were inlined (the Queue push method and the method of the NULL executor).

I wanted to put my brain on a frying pan after that.

Thanks!
 
Old 03-20-2009, 12:09 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Calling a function from a NULL pointer is generally safe (albeit malformed) if the class has no virtual functions or virtual base classes and the function doesn't access any data members or base classes (i.e. should have been a static function.) Did you check to see if the function called was static? Was it part of template code? It is possible to do this:
Code:
#include <stdio.h>

struct mystery
{
    void guess() const
    { if (!this) fprintf(stderr, "surprise!\n"); }
};

int main()
{ ((const mystery*) NULL)->guess(); }
It's very possible that the person writing the code wanted to access a function from a class he/she didn't author where the actual author should have made it a static function. In any case, you shouldn't do something like that intentionally under most circumstances.
Kevin Barry
 
Old 03-20-2009, 12:43 PM   #5
johnbellone
LQ Newbie
 
Registered: Mar 2009
Location: New Jersey
Distribution: Ubuntu, RedHat, Solaris, OS X
Posts: 9

Original Poster
Rep: Reputation: 0
Yeah, that's basically what I did.

Code:
class Object {
private:
Mutex mutex;
std::queue<string> q;
public:
  inline void push (string s) { this->lock(); this->q.push(s); this->unlock(); }
};

class Type {
private:
  Object * obj;
public:
  Type (void) { ... }
  Type (Object * obj) { this->obj = obj; }

  void doSomething (void) { this->obj->push (s); }
};

int
main (void) {
Object o;
Type t;

t.doSomething();
}
Of course I did not catch that the pointer to that object was NULL until I started testing my Mutex code trying to figure out why the hell it wouldn't be working. It took me a couple of hours to figure out that it was working, haha, and then I went into GDB and poked around more.

The mistake I made was not poking around more in the first place. Live and learn.

If someone else wrote the code I could complain them, but this is totally on my shoulders.

Last edited by johnbellone; 03-20-2009 at 12:44 PM.
 
  


Reply



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
Mutex destroy failure problem keen4linux Linux - Software 0 04-26-2007 11:20 PM
Pthread Problem IBall Programming 2 04-21-2006 02:05 AM
Pthread.... stuffs ... onwer of mutex rajsun Programming 1 07-26-2005 07:40 AM
How does pthread_mutex_lock() lock mutex in pthread icoming Programming 0 12-04-2004 08:54 AM
pthread mutex issue gauge73 Programming 6 04-20-2004 05:29 PM

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

All times are GMT -5. The time now is 03:14 AM.

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