DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: C++ Pthread Lock / Unlock in succession

  1. #1
    Join Date
    Jan 2008
    Posts
    100

    C++ Pthread Lock / Unlock in succession

    Hello,

    I have some software with a bunch o' threads trying to read / edit a std::vector. I then also have a continuous loop inside main()

    If I create a mutex but immediately lock it and then in the main loop do

    Code:
    pthread_mutex_unlock(&mutex);
    pthread_mutex_lock(&mutex);
    The other threads (which are also blocked by the mutex btw) do not unblock. They do however if I do...

    Code:
    pthread_mutex_unlock(&mutex);
    usleep(1); // could even be 0 and still works.
    pthread_mutex_lock(&mutex);
    Can anyone suggest a reason for this? Does usleep() (and I assume sleep() in Windows) do something to make all the other threads take priority?

    From my experience of this so far, this instant block/unblock makes for a pretty simple way of ensuring thread safety in an otherwise slightly complex design. I just want to make sure I am not kludging things too much :)

    Best Regards,

    Karsten
    Last edited by kpedersen; 07-12-2011 at 09:29 AM.

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    sleep puts the thread in which it is called to sleep --- that thread is no longer given any cpu time or resources or anything until the sleep delay is over. So when you sleep, there will be a 1 second or 1 milisecond or whatever the units are delay between the calls. I dunno if that answers your question... but all other threads will continue to run during the sleep delay.

    It looks unreliable. It looks like the unblock does not exist long enough for the threads to unblock normally on your particular machine because you reblock them too fast for the unblock to have any effect. Throwing a sleep in there allows it to happen. The sleep version is probably reliable but the other one... you risk the threads unblocking if you move the executable to another machine, or take it to release mode, or make some subtle change, etc.

Similar Threads

  1. How to Unlock the system??
    By faaz in forum C++
    Replies: 1
    Last Post: 05-09-2006, 04:11 AM
  2. Determining state of CAPS LOCK and NUM LOCK
    By David Williams in forum .NET
    Replies: 1
    Last Post: 04-04-2002, 10:31 AM
  3. ADO, Lock, SQL Server 7 & Oracle8i
    By Saiful in forum VB Classic
    Replies: 1
    Last Post: 09-11-2001, 08:35 AM
  4. Turn Num Lock, Caps Lock keys on and off with VB6
    By Chris Huynh in forum VB Classic
    Replies: 2
    Last Post: 03-01-2001, 09:57 AM
  5. Turn Num Lock, Caps Lock keys on and off with VB6
    By Chris Huynh in forum VB Classic
    Replies: 0
    Last Post: 03-01-2001, 01:52 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center