Threading Question?

Hi Experts,

I want to implement a method which doing some processing work in my program. The problem is that method must exist in another thread and it will be called quite frequently.

I know I can createThread everything I use the method, but the creation and destroy time is undesired. I can also make the thread to run in a while loop with a sleep inside there, but I don't want that.

My question is that is there anyway I can implemented a method that exist in a separate thread, if you don't call it, it will stay there forever unless you destroy it, but if you call its method, it will run again? and also I can directly call this method directly from my program, cause my knowledge on thread is only in createThread method?

Thank you for reading this post.

Best Regards
Ferdinand Ng


Comments

  • : Hi Experts,
    :
    : I want to implement a method which doing some processing work in my program. The problem is that method must exist in another thread and it will be called quite frequently.
    :
    : I know I can createThread everything I use the method, but the creation and destroy time is undesired. I can also make the thread to run in a while loop with a sleep inside there, but I don't want that.
    :
    : My question is that is there anyway I can implemented a method that exist in a separate thread, if you don't call it, it will stay there forever unless you destroy it, but if you call its method, it will run again? and also I can directly call this method directly from my program, cause my knowledge on thread is only in createThread method?
    :
    : Thank you for reading this post.
    :
    : Best Regards
    : Ferdinand Ng
    :
    :
    :


    You probably want to use events (see CreateEvent(), SetEvent() etc).
    Let main update the variables used by the thread, protected by a mutex.
    When it is time for the thread to execute, let main flag the thread by setting the event.

    Let the thread run in an infinite loop, starting with WaitForMultipleObjects(). Let it wait until both the event and the mutex are set.
  • Thanks Lundin,

    Thanks for your advice, I still have one question, if the thread is running in an infinite loop, then when I want to quit the program, can I just destroy the thread while the WaitForMultipleObjects() still waiting?

    Thank you for your advice again.

    Best Regards
    Ferdinand

  • : Thanks Lundin,
    :
    : Thanks for your advice, I still have one question, if the thread is running in an infinite loop, then when I want to quit the program, can I just destroy the thread while the WaitForMultipleObjects() still waiting?
    :
    : Thank you for your advice again.
    :
    : Best Regards
    : Ferdinand
    :
    :


    You could make another event called "timeToDie" or something. Or you can specify a timeout of WaitForMultipleObjects(), then check a flag. It is good practice to let the thread finish by itself. Avoid using TerminateThread().
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories