CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2009
    Posts
    252

    Locking a thread when is not needed and resume it when is needed.

    hello i want to pause a thread when is not needed and resume it when i need it latter.

    I tried using SuspendThread and ResumeThread but it behaves bugy when try to suspend and resume quickly, i been told that is not a good practice to use those functions if im not developing a debugger.

    im not trying to sincronize multiple threads i dont have troubles with that critical sections and mutex works just fine.

    i want to lock the thread so i could save system resources and resume when i need it.

    i been trying to use WaitForSingleObject but it "Locks" only when another thread obtains the mutex but i want to be able to lock the thread form it self.

    something like

    while(Lock==true){
    Sleep(50);
    }

    if i set the Lock to false from another thread this thread would resume. and pick another job.

    am i able to get this functionality by using WaitForSingleObject?

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Locking a thread when is not needed and resume it when is needed.

    The first thing that comes to my mind is a semaphore http://msdn.microsoft.com/en-us/libr...38(VS.85).aspx but on the other hand, why is the thread running if it doesn't have anything to do in the first place?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured