Archived

This topic is now archived and is closed to further replies.

ending a thread

This topic is 5592 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using the _beginthread function to start a thread and I would to know the best to end the thread before the function in the thread returns. I tired calling _endThread, but it closes the whole the program. In the program when the the thread starts I have a dialogue window pop up with a progress bar and when I close this window I want it to stop the thread. I just want to know if there is a better way of doing it than making a bool and putting if statements in the fucntion that return it early. Which I know works but I think there has to be a better way.

Share this post


Link to post
Share on other sites
I normally use CreateThread(..) to start up my threads. It returns a handle to the thread it created, and you can use that handle in a call to TerminateThread(ThreadHandle, 0) to close it down.

_beginthread returns a handle, but I dont know if its compatible with TerminateThread(..).

Alan

Share this post


Link to post
Share on other sites
TerminateThread() really should be used only in the most extreme of cases where other methods of termination (such as setting kill flags and having the thread check periodically) would be ineffective or impossible. Using TerminateThread() precludes the system from deallocating the thread's initial stack, and could possibly cause heap damage.

Getting back to your question, it's strange for _endThread() to be closing down the entire program unless the call is made from main program code. Are you placing it within the function the thread executes?

[edited by - johnnie2 on August 26, 2002 4:35:46 PM]

Share this post


Link to post
Share on other sites
Threading is touchy work. You should really let the thread clean itself up, and use thread-synchronization methods to alert the thread when it should shut itself down.

I''d suggest using Boost.Threads, as it really is a great cross-platform library for multi-threading.

Here''s an example:


  
#include <iostream>

#include <conio.h>

#include <boost/thread/thread.hpp>

#include <boost/thread/mutex.hpp>


using namespace std;
using namespace boost;


mutex g_mutex;
bool g_isdead = false;

void thread_func()
{
for (;;)
{
// check if we''re dead

mutex::scoped_lock l(g_mutex);
cout << "doing something... " << endl;
if(g_isdead)
break;
}
cout << "Terminating" << endl;
}

void main()
{
cout << "Press a key to start, press a key to quit" << endl;
getch();
thread worker(&thread_func);
getch();
{ // scoped to release the lock on the mutex, so that the thread doesn''t deadlock

mutex::scoped_lock l(g_mutex);
g_isdead = true;
}
worker.join(); // wait ''til thread is finished, then quit

cout << "Press any key to quit" << endl;
getch();
}

Share this post


Link to post
Share on other sites