Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

exiting threads

Status
Not open for further replies.

azntekkan

Programmer
Jun 5, 2003
10
0
0
CA
Hi, I'm working with threading and i'm having a little trouble exiting the threads.

I start up two threads using CreateThread.

The program runs, now i wish to stop the threads.

So the program is something like this

load program
create thread 1
create thread 2

wait for msg

if user clicks stop
{
stop both threads
}
if user clicks go
{
start threads
}


The thread call functions r while loops.

now i want to exit the threads from outside the thread call functions. If i use exitthread it just closes the windows. if i use terminatethread, it crashses the program after 3 stops and restarts. Please help me out and let me know if there is a way for me to exit a thread and then start that thread again repeatedly. P.S. i cannot use suspend or resume thread because i need the threads to restart.

Mike
 
That certainly depends on what is going on in those thread functions. Sometimes you can just use a Boolean value and set it to false when you want the thread function to exit. Other times you would need to use a synchronization object. Sometimes you need to close some resource like a socket handle.

-pete
 
In thread 1 is reading in from a file and placin the data in a pointer. Thread 2 is using the info from the pointer to display image frames using direct draw.

The main problem with using the boolean thing is that when i tell it to end it dosn't end right away. it continues for a few seconds then it ends
 
>> it continues for a few seconds then it ends

Ok, but that is then dependent on what ever activities are going on inside the thread functions. So your solution has to be specific to what your doing in the threads, it is not a generic thread question.

If I have a thread function that in a loop calls recv() on a blocking socket and I set some flag to false to exit the thread, it will never exit because it’s blocked on the recv() so it can’t check the flag. However if I close the socket the recv() call returns and then the thread can exit. This will take as long as the TCP/IP stack takes to perform it’s close operation so I am dependent on the TCP/IP subsytem and there is nothing I can do about it.


-pete
 
Pete's right - you have to be careful about what your thread is doing when you stop it.

If your design allows you can end the thread from outside by calling PostThreadMessage using the thread ID obtained from the CreateThread and posting the WM_QUIT message. Good Luck.

John
 
thnx, i'll try ur solution jfmclemore.

and palbano i think i get what ur saying.(or maybe not i'm somewhat a beginner)i don't know if this is wat u mean but my thread has a while loop
int thread()
{
while(exit==false)
{
read in from file
copy data to pointer
wait for event
}
return 0;
}

so i set exit=true then it should exit. it dosn't right away. because when i try to modfiy the data in the pointer it gives me that acess violation error
 
>> so i set exit=true then it should exit. it dosn't right away

Sure because it will only exit when it reaches the while() statement. For however long it is performing

[blue]read in from file
copydata to pointer[/blue]

It cannot exit. Now what about wait for event? Are you using WaitForSingleObject() with an event from CreateEvent()? If so then is the event signaled? If it is not then the WaitForSingleObject() call is going to block until the event is signaled keeping the while() statement from running and exiting the thread.

>> it gives me that acess violation error

That error is of course when you attempt to write to a memory address that has not been allocated by your process. It may or may not have anything to do with your thread exit question or code. Are you using a CRITICAL_SECTION to synchronized access to the memory bock?

-pete
 
>>Are you using WaitForSingleObject() with an event from CreateEvent()?

Yup, thats wat i'm using. and i am signaling it.

>>Are you using a CRITICAL_SECTION to synchronized access to the memory bock?

Nope. I allocated the memeory for the pointer before i create the thread.

I c wat ur sayin about exiting right away.
if i use sleep will it sleep jus the thread i'm callin or will it sleep all threads. so for example in the main thread i have

createThread(...)

let it run

then i want it to stop

so

PostThreadMessage(thread, WM_QUIT, threadID,NULL);
Sleep(1000);

i tried this but it dosn't seem to work. the thread still dosn't end quick enough.

Maybe i should explain more about my program. I making a media player which plays back a specific movie format.

And wat i'm trying to do is when the user press pause it will pause the movie. and then if the user clicks on a point in the timeline bar(scroll bar) the movie will go to that frame. So when the user clicks the pause button. i suspend the thread which reads in the data from the file and the thread which uses the data for directdraw.

But when i want to go the a specific frame i have to restart the threads becasue it is difficult to determine where the thread paused at. so far i haven't been able to do anything with out either my program crashing. or the thread ending too slowly, for example i pause at frame 20, choose to skip to frame 40. when i click on frame 40 it should show frame 40, but since the display thread did not end quickly enough it shows frame 40 for a split second and then shows frame 20 again.
 
Well, we have had several multi-thread conversations in the C++ and Java forums in the past couple months. They are extremely difficult to discussed in a message based conversation due to the complex nature of muli-threading.

For example you just posted but since the display thread did not end quickly enough it shows frame 40 for a split second and then shows frame 20 again.

But in your example thread function you posted there is no mention of DirectX so it becomes confusing and difficult to follow and make any meaningful analysis. It may not be possible to provide any meaningful help to you in this format but we will continue for a little while longer.

If your doing frame based DirectX output, you should be checking the exit condition on every frame right? Your frame rate is in the vicinity of what… 60 frames per second? If that is the case then the thread should exit very quickly yes? If it is not I have no idea why.

If your not synchronizing access to memory shared by the different threads then how do you keep from accessing bad data or even unallocated memory?


-pete
 
well i have do syncronize a little. i have 2 pointers, when the read in thread is reading into the first pointer the display thread is using the 2nd pointer. and vise versa.

My frame rate is 30 frames per second.....oh wait........ OMG so thats wat i was doing wrong! inside my while loop i have a for loop to read in the pointer data! thats why it took a while for it to end. thnx for the help, greatly appricated :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top