• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 365
  • Last Modified:

Doubt on Worker thread

Hi,

I am working with single worker thread.
I started thread like this
CWinThread *pThread;
pThread = AfxBeginThread(MyThreadFunc,this);

and i stopped thread like this
TerminateThread(pThread,0);

I would like to start two worker threads at a time.
How can synchirnize two threads? How should i know wheather the first is running or not? is there any speacial methiods to know this.

Thanks
0
vihar123
Asked:
vihar123
  • 3
  • 3
  • 2
  • +1
1 Solution
 
Jaime OlivaresCommented:
There are many synchronization techniques, maybe will be better to you to read some articles, to have the whole panorama:
http://www.codeproject.com/threads/
0
 
AxterCommented:
Hi vihar123,
> >How can synchronize two threads? How should I know whether the first
> >is running or not? is there any special methods to know this.
You can use a CSemaphore as a counter to count the number of threads running

Exactly what are you trying to synchronize?

David Maisonave :-)
Cheers!
0
 
vihar123Author Commented:
Thank you,

Actually i would like to start two threads in my application. But strating of these two threads is in loop.
In one case i need to start second round in loop, only after two threads finishes their processes.
and in another case,  i have to stop the running thread(s) before starting the second round in loop.
And i need to know from one thread, whether second thread is running or not?


Looking forward for your reply.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mactep13Commented:
Try WaitForMultipleObjects() call.
By the way, TerminateThread IS NOT a good function!!!!
It is not safe!!!. The thread will terminate... period! It will not dealocate the resources and release memory... it will just die. This is not safe practice. Try using either a CEvent object to signal to the thread to die or set a bool to indicate to the thread that he is finished. Then, you can call ExitThread from within the thread. Remember, ExitThread can not be called outside the thread function!

Hope this helps,
Mactep
0
 
vihar123Author Commented:
Thank you,

I need to terminate the thread outside of thread function also. Thats why i used TerminateThread()  function.
Which function is better to terminate thread from anywhere in class?


0
 
AxterCommented:
>>I need to terminate the thread outside of thread function also. Thats why i used TerminateThread()  function.
>>Which function is better to terminate thread from anywhere in class?
It's better if you have your own thread terminate itself.

You can use CMutex class together with a  CSingleLock class to indicate to a thread that it should terminate.
Your thread should then check the CSingleLock::IsLocked() to determine if it should exit.

TerminateThread should be a last resort method, when the thread is locked, and there's no other means to signal to it to terminate on its own.
0
 
mactep13Commented:
There is no clean way to terminate the thread from outside. Your best bet is to use an event object. If that is too much for a thread, set a bool variable to false and then when you want to stop the thread, set it to true. In your thread, check for the bool and if it is set to true, call ExitThread().

Something like this:

main function:

bool bStopThread = false;

beginthread(ThreadFunc);

// Do some processing logic

if (NeedToKillThread())
{
     bStopThread = true;
}

}

 Inside thread:

ThreadFunc()
{
       // Do something
        if (bStopThread)
              ExitThread();

        // Do something
        if (bStopThread)
              ExitThread();
        // Do Something
        // and so on
}

or if the thread is in the loop

ThreadFunc()
{
            while(bContinue) // Or something
             {
                     // Do SOmething
                     if (bStopThread)
                           ExitThread();
                      Sleep(100);
            }
           return 0;
}
0
 
vihar123Author Commented:
Thank you for your reply.

UINT ImagesSavingThread(LPVOID lParam)
{
   CPropertyPage3* cvs=(CPropertyPage3*)lParam;
   bool check = cvs->OnSaveImages();
}

This is my thread function, how can i call ExitThread() ?, because i am calling another function from thread..


Looking forward for your reply.
0
 
mactep13Commented:
You can call ExitThread function from within the OnSaveImages(). That should work.

Hope this helps,
Mactep
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now