Solved

threading problems again..

Posted on 1998-08-20
11
255 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
Comment Utility
oops _exitthreadex should ofcourse be _endthreadex ...
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
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
Comment Utility
ok i figured that, but is there no way to terminate it from the
outside?
0
 
LVL 6

Expert Comment

by:snoegler
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 6

Expert Comment

by:snoegler
Comment Utility
Sorry i double-clicked on 'Submit' ... :)
0
 

Author Comment

by:webrage
Comment Utility
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
Comment Utility
Hello!
Your code will work so:
startThread(){
 handle=_beginthreadex(thrFunc, ....);
 /* store handle wherever */
}
stopThread(){
_endthreadex(handle);
}

Sergey
0
 

Author Comment

by:webrage
Comment Utility
oops _exitthreadex should ofcourse be _endthreadex ...
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
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
Comment Utility
hmm oops i totally agree with chensu.. Im sorry for giving points to wrong person... :(

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now