Solved

threading problems again..

Posted on 1998-08-20
11
266 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

831 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