CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Dec 2002
    Location
    Candia, NH
    Posts
    374

    Creating an Event across two threads

    In Thread A I want to wait for the value of some parameter in Thread B to be set to a certain value and if it doesn't happen in 10 seconds, the wait is terminated.

    So, in Thread A I have a

    WaitForSingleEvent(hevent, 10000);

    The problem is how to use CreateEvent() in this case across two threads.

    Thread B is an audio signal detection routine that is continuously running. The signal strength is continuously updated and placed in a variable 'signalstrength'.

    Thread A is waiting to use the channel Thread B is monitoring, and it needs to wait until the signal strength drops below a certain value before using the channel.

    So the event I want to wait upon is when 'signalstrength' drops below the threshold value.

    Can this be done without defining a global 'hevent = CreateEvent()'? Thread B is implemented as an instance of a class object.

    Thanks,

    Brian Reinhold

  2. #2
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Creating an Event across two threads

    Quote Originally Posted by Gyannea
    So, in Thread A I have a

    WaitForSingleEvent(hevent, 10000);
    There is no such thing as WaitForSingleEvent... Perhaps, you mean WaitForSingleObject!
    Quote Originally Posted by Gyannea
    The problem is how to use CreateEvent() in this case across two threads.
    The way I see it - you need one Event that is signalled in one thread (B), and in waited upon by another thread (A).

    So, you need only one CreateEvent, but have to make the HANDLE returned by the same available in both threads.
    Quote Originally Posted by Gyannea
    Can this be done without defining a global 'hevent = CreateEvent()'? Thread B is implemented as an instance of a class object.
    Yes... Threads Functions accept a parameter. So, the Event can be created by the main thread using CreateEvent, and the HANDLE of the same be passed on to both Thread A and Thread B as the parameter.

    Thread A can wait on this HANDLE whilst Thread B can do a SetEvent on it when the time is ripe.

    In this post you will find a detailed explanation on how events can be used.

  3. #3
    Join Date
    Dec 2002
    Location
    Candia, NH
    Posts
    374

    Re: Creating an Event across two threads

    Thanks Sidhartha!

    Yes, I did mean 'Object' and not 'Event', I was just thinking events.

    Okay, that is what I thought; effectively the handle is a global; you just "pass" them to the threads.

    I was thinking instead of creating the event and thus the handle when I create and initialize the instance that creates thread B since it runs continuously.

    Then in Thread A I would have something like

    WaitForSingleObject(b->GetEventHandle(), 10000);

    Then in thread B I would call SetEvent() or ResetEvent() depending upon the value of the threshold. The function 'b->GetEventHandle()' would have to be a public member of the class that generates Thread B of course.


    Does this approach sound okay or am I a step behind?

    Brian

  4. #4
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Creating an Event across two threads

    Quote Originally Posted by Gyannea
    Then in Thread A I would have something like

    WaitForSingleObject(b->GetEventHandle(), 10000);

    Then in thread B I would call SetEvent() or ResetEvent() depending upon the value of the threshold. The function 'b->GetEventHandle()' would have to be a public member of the class that generates Thread B of course.
    As such this seems okay... Save for the fact that how will Thread A get instance "b"?

    From the Design perspective, are you sure that Thread A working with the thread class B is a good idea given that the HANDLE is all they have in common?

    One way is to have the HANDLE out either of the two, but still without keeping it global. Like this -
    Code:
    class CThreadsSharedInfo
    {
    public:
      CThreadsSharedInfo () {};
    
      static HANDLE GetEvent ()
      {
    	if (!CThreadsSharedInfo::m_hEvent) 
    	  CThreadsSharedInfo::m_hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
    
    	return CThreadsSharedInfo::m_hEvent;
      };
    
    
    private:
      static HANDLE m_hEvent;
    };
    
    // static member initialization inside the CPP
    HANDLE CThreadsSharedInfo::m_hEvent = NULL;
    And Thread 1 can wait on it, like this -
    Code:
    	WaitForSingleObject (CThreadsSharedInfo::GetEvent (), 10000);
    Thread 2 can signal the event, like this -
    Code:
      SetEvent (CThreadsSharedInfo::GetEvent ());
    This is one way to reach the end, off the many possible ways.

    As you can see, CThreadsSharedInfo can also be made a Singleton class; be used to share more information between threads than just the handle, via accessor methods; and it would be relatively easy to implement thread synchronization over objects it shares using this alternative.
    Last edited by Siddhartha; November 1st, 2005 at 12:28 PM.

  5. #5
    Join Date
    Dec 2002
    Location
    Candia, NH
    Posts
    374

    Re: Creating an Event across two threads

    Hmmm. Didn't really think of that approach. But again, it really looks like the singleton class of this shared event is still just a global...except that it is protected (I couldn't set it to a vlaue by mistake). I guess that's the point. I create the singleton class at the start of the program and use it as needed and it will be protected form external corruption.

    As far as Thread A accessing the function 'b->GetEventHandle()', shouldn't it be possible if I declare 'GetEventHandle()' a public member of the class that creates thread B?

    THanks for the help, by the way!

    Brian

  6. #6
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Creating an Event across two threads

    Quote Originally Posted by Gyannea
    As far as Thread A accessing the function 'b->GetEventHandle()', shouldn't it be possible if I declare 'GetEventHandle()' a public member of the class that creates thread B?
    I have answered this question in my previous post... How does Thread A have instance "b" to invoke GetEventHandle on it?

    If it does, then this is a functional alternative, and it should work so long as the HANDLE returned is a valid one.
    Quote Originally Posted by Gyannea
    THanks for the help, by the way!
    You are welcome...

  7. #7
    Join Date
    Dec 2002
    Location
    Candia, NH
    Posts
    374

    Re: Creating an Event across two threads

    I was assuming it would have access the following manner:

    (1) When I create the instance that creates thread B, there is a private variable 'HANDLE hevent'.

    Then in the initialization I have

    hevent = CreateEvent(....)

    Then a public function in the class is

    HANDLE GetEventHandle(void)
    {
    return(hevent);
    }

    If the instance of the class is 'b', then I assumed that would be a pointer to the handle that the API wait function could use. Maybe I am off by a level of pointing?

    Brian

  8. #8
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Creating an Event across two threads

    Quote Originally Posted by Gyannea
    I was assuming it would have access the following manner:
    You haven't explained access to Thread A, you have only shown an "accessor method" in the form of GetEventHandle.
    Quote Originally Posted by Gyannea
    (1) When I create the instance that creates thread B, there is a private variable 'HANDLE hevent'.

    Then in the initialization I have

    hevent = CreateEvent(....)

    Then a public function in the class is

    HANDLE GetEventHandle(void)
    {
    return(hevent);
    }
    So far so good....
    Quote Originally Posted by Gyannea
    If the instance of the class is 'b', then I assumed that would be a pointer to the handle that the API wait function could use. Maybe I am off by a level of pointing?
    Now, the question is - "How did this pointer "b" get to Thread A?"

    And if it did somehow, then of course the method will work.

    Code:
    UINT ThreadA (LPVOID pData)
    {
    	b->GetEventHandle ();  // <--- Where did "b" come from?
    }
    Do you get it now?

    Clearly, someone needs to pass "b" to ThreadA, or ThreadA needs to create B.

  9. #9
    Join Date
    Dec 2002
    Location
    Candia, NH
    Posts
    374

    Re: Creating an Event across two threads

    Yes, it worked. Thread A knows of 'b' since it is a global instance (the process runs continuously in the background monitoring the received radio signal).

    Thanks for all your help.

    Brian

  10. #10
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Creating an Event across two threads

    You are welcome...

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