Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. WaitForSingleObject Query

WaitForSingleObject Query

Scheduled Pinned Locked Moved C / C++ / MFC
c++databasequestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vikas K
    wrote on last edited by
    #1

    Hi, I have 2 variants of usage of WaitForSingleObject. Pls tell me which one is the proper usage? Version1:

    #include #include HANDLE hEvent;

    DWORD WINAPI SampleThread(LPVOID iValue)
    {

    int iFinish = 120;
    for(int i=100;i<=iFinish;i++)
    cout<
    

    Version2:

    // WaitForSingleObj.cpp : Defines the entry point for the console application. // #include "stdafx.h" // WaitForSingleObject Sample.cpp #include #include DWORD WINAPI SampleThread(LPVOID iValue) { int iFinish = 120; for(int i=100;i<=iFinish;i++) cout<

    G R 2 Replies Last reply
    0
    • V Vikas K

      Hi, I have 2 variants of usage of WaitForSingleObject. Pls tell me which one is the proper usage? Version1:

      #include #include HANDLE hEvent;

      DWORD WINAPI SampleThread(LPVOID iValue)
      {

      int iFinish = 120;
      for(int i=100;i<=iFinish;i++)
      cout<
      

      Version2:

      // WaitForSingleObj.cpp : Defines the entry point for the console application. // #include "stdafx.h" // WaitForSingleObject Sample.cpp #include #include DWORD WINAPI SampleThread(LPVOID iValue) { int iFinish = 120; for(int i=100;i<=iFinish;i++) cout<

      G Offline
      G Offline
      GuyM
      wrote on last edited by
      #2

      In the first version the call to CreateEvent(...) should come before CreateThread(...), because the new thread might start and end before the original thread reaches CreateThread() ... Otherwise, both are OK. Hope this helps ...

      1 Reply Last reply
      0
      • V Vikas K

        Hi, I have 2 variants of usage of WaitForSingleObject. Pls tell me which one is the proper usage? Version1:

        #include #include HANDLE hEvent;

        DWORD WINAPI SampleThread(LPVOID iValue)
        {

        int iFinish = 120;
        for(int i=100;i<=iFinish;i++)
        cout<
        

        Version2:

        // WaitForSingleObj.cpp : Defines the entry point for the console application. // #include "stdafx.h" // WaitForSingleObject Sample.cpp #include #include DWORD WINAPI SampleThread(LPVOID iValue) { int iFinish = 120; for(int i=100;i<=iFinish;i++) cout<

        R Offline
        R Offline
        Roger Stoltz
        wrote on last edited by
        #3

        It looks as if you want to know when the worker thread has finished, right? Your first alternative waits on an event to be signalled from within the worker thread. Note that as already advised you have to create the event before it is used or you'll be facing a runtime error eventually. By using this technique you will be informed when the thread is about to finish. My point is that by the time the event gets signalled and your waiting function is released, the worker thread may or may not have finished completely. You should wait on the thread handle instead, like you do in your second alternative. The thread handle will enter "signalled" state when the thread has finished completely and has been unloaded from the system. Read more about worker threads here[^].


        "It's supposed to be hard, otherwise anybody could do it!" - selfquote
        "High speed never compensates for wrong direction!" - unknown

        V 1 Reply Last reply
        0
        • R Roger Stoltz

          It looks as if you want to know when the worker thread has finished, right? Your first alternative waits on an event to be signalled from within the worker thread. Note that as already advised you have to create the event before it is used or you'll be facing a runtime error eventually. By using this technique you will be informed when the thread is about to finish. My point is that by the time the event gets signalled and your waiting function is released, the worker thread may or may not have finished completely. You should wait on the thread handle instead, like you do in your second alternative. The thread handle will enter "signalled" state when the thread has finished completely and has been unloaded from the system. Read more about worker threads here[^].


          "It's supposed to be hard, otherwise anybody could do it!" - selfquote
          "High speed never compensates for wrong direction!" - unknown

          V Offline
          V Offline
          Vikas K
          wrote on last edited by
          #4

          Thanks guys.. your answers really helped me. I had a doubt if I could pass the handle of thread instead of the handle of an event. Vikas

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups