Link to home
Start Free TrialLog in
Avatar of sunshine737
sunshine737

asked on

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
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

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/
Avatar of Axter
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!
Avatar of sunshine737
sunshine737

ASKER

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.
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
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?


>>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.
ASKER CERTIFIED SOLUTION
Avatar of mactep13
mactep13

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
You can call ExitThread function from within the OnSaveImages(). That should work.

Hope this helps,
Mactep