Thread: pthread_mutex

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    pthread_mutex

    I read that when porting Windows code using EnterCriticalSection etc that you must use pthread_mutex_lock and friends but I'm wondering if there are any differences. In windows the advantage of using critical sections is that they require no switch to kernel mode. Is that the same for the linux pthread mutex?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't see why a pthread_mutex would need to enter the kernel for the locking itself - of course, if some other thread holds the lock, you probably will enter a "sleep" or "wait" function that in turn involves the scheduler.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    in windows the mutex functions do that because it is possible to synchronize processes and only the kernel knows about the processes. I thought maybe its the same in linux

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Nope, the posix primitives are not inter-process.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by KIBO View Post
    I read that when porting Windows code using EnterCriticalSection etc that you must use pthread_mutex_lock and friends but I'm wondering if there are any differences. In windows the advantage of using critical sections is that they require no switch to kernel mode. Is that the same for the linux pthread mutex?
    In theory there need not be a switch to kernel mode when locking/unlocking an uncontended mutex. Obviously, the kernel must be informed in the following cases:

    1. A process tries to lock an already-locked mutex, which means the process attempting to acquire the lock needs to go to sleep.
    2. A process unlocks a mutex which has other processes waiting on it. The kernel has to wake them up.

Popular pages Recent additions subscribe to a feed