Solved

threading problems again..

Posted on 1998-08-20
11
272 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

790 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