Thread: pthread in Linux vs Windows

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    4

    Unhappy pthread in Linux vs Windows

    Hi,

    I have some questions regarding pthread related to windows and linux. pthread is supported in both linux and windows. In multithreading environment we use mutex/critical section/semaphore to synchronize operations on shared resource.

    This is specific to windows

    Mutex :
    * kernel objects
    * multi-process capability
    * timeout support for signals
    * slow performance compare to critical section

    Critical Section :
    * user level objects
    * no muti-process capability
    * infinite wait for signals
    * high performance

    Currently I'm optimizing a code which used both _beginthread (Win API) and pthread_create (pthread API). As well as they used pthread mutex, CRITICAL_SECTION, and Windows Mutex.

    My questions are,
    1. What is the relation between thread and mutex/critical section?
    2. Using of pthread_mutex_t in _beginthread() or CRITICAL_SECTION in pthread_create() will have any impact?
    3. Why mutex initialization creating new handle? Why critical section initialization not creating events?

    Please help me out from this.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    1) None, as such. Threads are, well, threads. Mutexes and Win32 Critical Sections are synchronization objects.
    2) Shouldn't have any. In other words, unless whatever pthread library you're using is doing some seriously weird stuff, they should operate without problems.
    3) Can you clarify the questions?
    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

  3. #3
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Quote Originally Posted by richardforc View Post
    3. Why mutex initialization creating new handle?
    Because that's how you interact with kernel objects on Windows. If you're asking why you get a handle rather than a mutex object to interact with then lookup the benefits of Handle-Body idiom or pimpl which equates to the same thing.

    Why critical section initialization not creating events?
    It does
    Code:
    // from WinNT.h
    typedef struct _RTL_CRITICAL_SECTION {
        PRTL_CRITICAL_SECTION_DEBUG DebugInfo;
    
        //
        //  The following three fields control entering and exiting the critical
        //  section for the resource
        //
    
        LONG LockCount;
        LONG RecursionCount;
        HANDLE OwningThread;        // from the thread's ClientId->UniqueThread
        HANDLE LockSemaphore;
        ULONG_PTR SpinCount;        // force size on 64-bit systems when packed
    } RTL_CRITICAL_SECTION, *PRTL_CRITICAL_SECTION;
    typedef RTL_CRITICAL_SECTION CRITICAL_SECTION;
    The LockSemaphore member is an event that is waited on if the lock is held by another thread. For more information: Critical sections under Windows

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    4

    Question

    Quote Originally Posted by CornedBee View Post
    1) None, as such. Threads are, well, threads. Mutexes and Win32 Critical Sections are synchronization objects.
    I read that unlocking sync object(especially pthread mutex) produce a signal. If it is so, what will happen if no thread waiting to acquire lock. Whether any special handling is required to handle that signal or we can leave it as it is (windows will take care that).

    3) Can you clarify the questions?
    Question is regarding opening 'event' handle in pthread mutex (you can see it using handle.exe). One new event handle is created when calling pthread_mutex_init(). But, InitializeCriticalSection() not creating such a 'event' handle.
    Last edited by richardforc; 02-06-2011 at 01:58 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. windows .dll vs. linux .so - global static objects
    By pheres in forum C++ Programming
    Replies: 11
    Last Post: 11-23-2010, 01:29 PM
  2. Thinking of upgrading to linux...
    By Yarin in forum General Discussions
    Replies: 37
    Last Post: 07-24-2009, 11:40 AM
  3. Build linux on windows
    By baash05 in forum Linux Programming
    Replies: 6
    Last Post: 02-19-2008, 10:12 PM
  4. Why can't Windows run Linux binary executables?
    By Kleid-0 in forum Tech Board
    Replies: 30
    Last Post: 12-04-2005, 11:44 PM
  5. Linux and Windows Duel Boot
    By The15th in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-26-2002, 04:59 AM

Tags for this Thread