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

threading problems again..

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
webrage
Asked:
webrage
  • 5
  • 3
  • 2
  • +1
1 Solution
 
webrageAuthor Commented:
oops _exitthreadex should ofcourse be _endthreadex ...
0
 
chensuCommented:
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
 
webrageAuthor Commented:
ok i figured that, but is there no way to terminate it from the
outside?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
snoeglerCommented:
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
 
snoeglerCommented:
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
 
snoeglerCommented:
Sorry i double-clicked on 'Submit' ... :)
0
 
webrageAuthor Commented:
ok i think im on a track now thnx for the help..
send a answer now and i give u 50pts!
0
 
snemanovCommented:
Hello!
Your code will work so:
startThread(){
 handle=_beginthreadex(thrFunc, ....);
 /* store handle wherever */
}
stopThread(){
_endthreadex(handle);
}

Sergey
0
 
webrageAuthor Commented:
oops _exitthreadex should ofcourse be _endthreadex ...
0
 
chensuCommented:
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
 
webrageAuthor Commented:
hmm oops i totally agree with chensu.. Im sorry for giving points to wrong person... :(

0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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