LinuxQuestions.org
Visit Jeremy's Blog.
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-24-2012, 10:56 PM   #1
macarthor
Member
 
Registered: Jun 2004
Posts: 37

Rep: Reputation: 15
Linux pthread mutex deadlock sometimes get assert, while sometimes not


I encountered an odd question about linux mutex lock, with Fedora 16 x64 as the development platform.

Code:
pthread_mutex_t lock; // in my practical project, this variable is not global, but packed in a struct. here is for demo.

void init()
{
    pthread_mutex_init(&lock, NULL); // the mutex is initialized as PTHREAD_MUTEX_NORMAL.
}

void EpollThread()
{
    uint64_t unlocknum = 0;
    ......;

    while (1)
    {
        ......;

        // I use a counter, and print some parameters of the mutex. The params are maintained by the kernel, I think.
        pthread_mutex_unlock(&lock); // to notify ReadThread() that it can read data.
        unlocknum++;
        print(unlocknum);
        print(lock.__data.__lock, lock.__data.__owner, lock.__data.__nusers);

        ......;
    }
}

void ReadThread()
{
    uint64_t locknum = 0;
    ......;

wait_lock:
    pthread_mutex_lock(&lock);
    locknum++;
    print(locknum);
    print(lock.__data.__lock, lock.__data.__owner, lock.__data.__nusers);

    ......; // read data and some other processes.

    goto wait_lock;
}
In the practical test, I found the following odd things:

0. NOTE: the following questions are NOT IN ONE STACK.

1. After printing "_lock=0", it suddenly prints "_lock=2". I'm quite curious about this because there's only ONE thread calling pthread_mutex_lock().

2. A "__lock=0" is right after a pthread_mutex_lock(), and I know this according to 2 logs "locknum=xxx" & "__lock=0". Perhaps there's a pthread_mutex_unlock() between the above 2 logs?(the log subsystem is working in 2 threads)

3. An assert() occurs after a pthread_mutex_lock(). Through GDB, I can see, in the stack, the assert is in pthread_mutex_lock() "assert(__owner==0)". BUT, in the upper layer of THE stack where I call pthread_mutex_lock(), I get "lock._data._owner=0", that is, it cannot trigger the assert?!
 
Old 03-25-2012, 08:26 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,774

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
If the mutex type is PTHREAD_MUTEX_NORMAL, deadlock detection shall not be provided. Attempting to relock the mutex causes deadlock. If a thread attempts to unlock a mutex that it has not locked or a mutex which is unlocked, undefined behavior results.

http://linux.die.net/man/3/pthread_mutex_lock
You can't use locks that way, what you really want here is a semaphore.
 
Old 03-25-2012, 09:46 AM   #3
sree_ec
Member
 
Registered: Sep 2010
Location: world
Distribution: Ubuntu 12.04LTS
Posts: 76

Rep: Reputation: 5
Quote:
Originally Posted by macarthor View Post
I encountered an odd question about linux mutex lock, with Fedora 16 x64 as the development platform.

Code:
pthread_mutex_t lock; // in my practical project, this variable is not global, but packed in a struct. here is for demo.

void init()
{
    pthread_mutex_init(&lock, NULL); // the mutex is initialized as PTHREAD_MUTEX_NORMAL.
}

void EpollThread()
{
    uint64_t unlocknum = 0;
    ......;

    while (1)
    {
        ......;

        // I use a counter, and print some parameters of the mutex. The params are maintained by the kernel, I think.
        pthread_mutex_unlock(&lock); // to notify ReadThread() that it can read data.
        unlocknum++;
        print(unlocknum);
        print(lock.__data.__lock, lock.__data.__owner, lock.__data.__nusers);

        ......;
    }
}

void ReadThread()
{
    uint64_t locknum = 0;
    ......;

wait_lock:
    pthread_mutex_lock(&lock);
    locknum++;
    print(locknum);
    print(lock.__data.__lock, lock.__data.__owner, lock.__data.__nusers);

    ......; // read data and some other processes.

    goto wait_lock;
}
In the practical test, I found the following odd things:

0. NOTE: the following questions are NOT IN ONE STACK.

1. After printing "_lock=0", it suddenly prints "_lock=2". I'm quite curious about this because there's only ONE thread calling pthread_mutex_lock().

2. A "__lock=0" is right after a pthread_mutex_lock(), and I know this according to 2 logs "locknum=xxx" & "__lock=0". Perhaps there's a pthread_mutex_unlock() between the above 2 logs?(the log subsystem is working in 2 threads)

3. An assert() occurs after a pthread_mutex_lock(). Through GDB, I can see, in the stack, the assert is in pthread_mutex_lock() "assert(__owner==0)". BUT, in the upper layer of THE stack where I call pthread_mutex_lock(), I get "lock._data._owner=0", that is, it cannot trigger the assert?!
mutex lock and unlock must happen in a single thread. For your purpose you can use semaphores
 
  


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
[SOLVED] pthread threads and mutex naaman Programming 1 02-07-2011 02:50 AM
Pthread mutex doubt rajesh1978 Programming 2 09-17-2010 09:34 AM
pthread and mutex question Michael23 Programming 2 11-16-2009 03:53 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 04:48 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