dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: pthread_mutex_lock fails

  1. #1
    Join Date
    Jul 2007
    Posts
    15

    pthread_mutex_lock fails

    Hi,

    I have linked statically libodbc++ (libodbc++-mt.a) with my CPP application and connect to MySQL from various threads simultaneously from my application. At some point my application crashes with the following error,

    terminate called after throwing an instance of 'odbc::SQLException'
    what(): [libodbc++]: OS error, mutex lock failed
    HY000 : [libodbc++]: OS error, mutex lock failed :: [Open DB] : IN DB /home/test/Users/Srini/xxxxxxx/dbnamexxx terminate called recursively
    HY000 : [libodbc++]: OS error, mutex lock failed :: [Open DB] : IN DB /home/test/Users/Srini/xxxxxxx/dbnamexxx terminate called recursively
    HY000 : [libodbc++]: OS error, mutex lock failed :: [Open DB] : IN DB /home/test/Users/Srini/xxxxxxx/dbnamexxx
    Aborted

    Seems the pthread_mutex_lock() in src/threads.cpp fails with error and libodbc throws exception and hence application crashes (running in my application in debug mode). I have checked the return value of pthread_mutex_lock() and it is EINVAL (22) which means Invalid Argument.

    The details of my setup is as follows,

    OS : Cent OS 5.3
    libodbc++ : v0.2.3
    unixODBC : v2.2.15pre
    MySQL Driver : v3.51.27
    MySQL Server : v5.1.39

    My odbcinst.ini file is as follows,

    -------------------8<----------------------
    [ODBC]
    Trace = No
    Trace File = /tmp/sql.log
    Pooling = Yes

    [MySQL]
    Description =
    Driver = /usr/local/lib/libmyodbc3-3.51.27.so
    Driver64 =
    Setup = /usr/local/lib/libmyodbc3S-3.51.27.so
    Setup64 =
    UsageCount =1
    CPTimeout =300
    CPReuse =1
    Threading =0
    ------------------->8----------------------

    My configure option for libodbc++ is as follows,

    ./configure --enable-static=yes --with-odbc=/home/path/to/unixodbc/unixODBC-2.2.15pre/DriverManager/.libs/ --enable-threads=yes

    I have also tested with libodbc++ v0.2.5 and still I face the same issue.

    Has anyone experienced similar problem. Any help would be greatly appreciated.

    Regards,
    Srinivasan

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,153

    Re: pthread_mutex_lock fails

    According to the pthread_mutex_lock man page:
    EINVAL
    The mutex was created with the protocol attribute having the value PTHREAD_PRIO_PROTECT and the calling thread's priority is higher than the mutex's current priority ceiling.
    OR
    The value specified by mutex does not refer to an initialized mutex object.
    Are you sure the mutex you pass is properly initialized?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Jan 2009
    Posts
    1,689

    Re: pthread_mutex_lock fails

    Code:
    pthread_mutex_t mylock;
    pthread_mutex_init(&mylock, NULL);

  4. #4
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,153

    Re: pthread_mutex_lock fails

    It would be a good idea to check the return value of pthread_mutex_init.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  5. #5
    Join Date
    Jul 2007
    Posts
    15

    Re: pthread_mutex_lock fails

    pthread_mutex_init() is success and returns 0.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: pthread_mutex_lock fails

    You're going to have to show more context. Just because you thought that mutex was initialized with that code doesn't mean it definitely was. A trivial example:

    Code:
    struct Locker
    {
        pthread_mutex_t mutex;
        Locker()
        {
            pthread_mutex_init(&mutex,NULL);
        }
        ~Locker()
        {
            pthread_mutex_destroy(&mutex);
        }
        void lock()
        {
            pthread_mutex_lock(&mutex);
        }
        void unlock()
        {
            pthread_mutex_unlock(&mutex);
        }
    }
    No way this can fail, right? Wrong.
    Code:
    void func(Locker lock)
    {
        lock.lock();
        cerr << "Do stuff requiring lock\n";
        lock.unlock();
    }
    
    int main()
    {
        Locker lock;
        func(lock);
    
        lock.lock();////whoops! pthread_mutex_lock will fail here. Can you figure out why?
        // do more stuff requiring lock
        lock.unlock();
    }
    Last edited by Lindley; February 18th, 2011 at 10:11 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)