Solved

threading problems again..

Posted on 1998-08-20
11
288 Views
Last Modified: 2008-02-01
My thread wont stop...
my source will now explain my problem:

startThread(){
 handle=_beginthreadex(thrFunc);
}

stopThread(){
 getExitCode(handle,&ec);
_exitthreadex(ex);  //this doesnt stop my thread..
}

thrFunc(){
 while(1){
  //checking a directory for new files...
 }
}

 
 
0
Comment
Question by:webrage
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
11 Comments
 

Author Comment

by:webrage
ID: 1170731
oops _exitthreadex should ofcourse be _endthreadex ...
0
 
LVL 23

Expert Comment

by:chensu
ID: 1170732
Shouldn't it be _endthreadex instead of _exitthreadex?

_endthreadex terminates the thread that is calling _endthreadex. You should call it in thrFunc() to terminate it.
0
 

Author Comment

by:webrage
ID: 1170733
ok i figured that, but is there no way to terminate it from the
outside?
0
Independent Software Vendors: 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!

 
LVL 6

Expert Comment

by:snoegler
ID: 1170734
TerminateThread() would help, if you are using windows (i assume that because of
_beginthread ... and so on). But it is clearly stated in the docs that this command should
be used only under extreme circumstances. The clean way would be to implement an
'HEVENT' (CreateEvent()), checking if it is signaled and if yes terminated the thread from
inside.
If you are using MFC, another possibility is using a CWinThread, which has a own message
loop - using PostThreadMessage() you can send a stop message and its handler then
calls the _exitthread() function.
0
 
LVL 6

Expert Comment

by:snoegler
ID: 1170735
TerminateThread() would help, if you are using windows (i assume that because of
_beginthread ... and so on). But it is clearly stated in the docs that this command should
be used only under extreme circumstances. The clean way would be to implement an
'HEVENT' (CreateEvent()), checking if it is signaled and if yes terminated the thread from
inside.
If you are using MFC, another possibility is using a CWinThread, which has a own message
loop - using PostThreadMessage() you can send a stop message and its handler then
calls the _exitthread() function.
0
 
LVL 6

Expert Comment

by:snoegler
ID: 1170736
Sorry i double-clicked on 'Submit' ... :)
0
 

Author Comment

by:webrage
ID: 1170737
ok i think im on a track now thnx for the help..
send a answer now and i give u 50pts!
0
 

Accepted Solution

by:
snemanov earned 100 total points
ID: 1170738
Hello!
Your code will work so:
startThread(){
 handle=_beginthreadex(thrFunc, ....);
 /* store handle wherever */
}
stopThread(){
_endthreadex(handle);
}

Sergey
0
 

Author Comment

by:webrage
ID: 1170739
oops _exitthreadex should ofcourse be _endthreadex ...
0
 
LVL 23

Expert Comment

by:chensu
ID: 1170740
I agree with snoegler. snemanov's answer is wrong. Why did you accept it?

void _endthreadex( unsigned retval );

Return Value

None

Parameter

retval Thread exit code

0
 

Author Comment

by:webrage
ID: 1170741
hmm oops i totally agree with chensu.. Im sorry for giving points to wrong person... :(

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question