can't close a thread - help
ello,
I have got a thread created like this:
SoftwareThreadHandle=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)SoftwareThread,hWnd,0,&SoftwareThreadId);
it's a nice thread and it closes under win2k without a problem using this:
TerminateThread(SoftwareThreadHandle,0);
but once I run PPC emu and use it with my GAPI routines Terminating a thread doesn't help.... unless I change its priority to Abovenormal with this:
SetThreadPriority(SoftwareThreadHandle,THREAD_PRIORITY_ABOVE_NORMAL);
why is it so??? how to get around it? Do I have to use ExitThread from the threadloop? (it's an infinite loop updating graphics as needed and getting ExitThread somewhere between that doesn't seem like a good idea to me:)
Can anyone help my little Mr.thread a bit?:)
thnx for all help!
It should die when you return from it.
Also, there is something wierd about the way wince does threads. The only way I've gotten it to work is like this:
- Code:
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)TheThread, (LPVOID)&hWnd, 0, NULL);
DWORD TheThread(HWND *hWnd)
{
return TLoop();
}
int TLoop(void)
{
int i;
while (Alive)
{
if (NGnow)
{
NGnow = 0;
i = NewGame(MhWnd);
}
}
return i;
}
That's the way I get em to work. Granted it's kinda sloppy, and requires a couple of global variables, but, hey, it works

If I understand you correctly, you aren't able to terminate the thread properly?
Using TerminateThread is not very good design. You should have the thread terminate itself by simply returning from the ThreadProc, or calling ExitThread(). Rather than bumping up the thread priority, have you tried adding a call to Sleep() immediately after calling TerminateThread()?
Having a separate thread that is rendering graphics on a single proc system is poor software design (if you're concerned about performance and code maintainence). With only one processor, the only time you should be using multiple threads is if the thread is waiting on some external event and is sleeping (like a socket connection).
superman,
I've never seen the problem you're referring to. What happens if your code is contained within ThreadProc instead of in another function? Are you saying the code isn't executed?
From looking at your code I'd question your use of thread too. Spinning on global variables in a background thread is one of the worst things you can do.
Digby:hmm, now you make me wonder. I have always thought that few threads can increase dramatically overall software useability. For example my application might take even several seconds to render a scene. Should I cut-off user from any control over the application till it finishes? Not mentioning systems that draw images all the time. As far as i know multithreading is often used in modern games etc. (like one thread for key/mouse/joystick control, one thread for game graphics,one thread for network connection control and one thread for sound/music routines)
My thread makes usage of Sleep thru as I rerender images only after changes has been made. I'm waiting for key/mouse events in the main thread.
PS.adding a Sleep(50) call after TerminateThread() works fine. thanks
superman:my code compiles fine, looks like this:
DWORD SoftwareThread(LPVOID param)
{
while (1)
{
if (redraw==TRUE)
{
//redraw code here
}
else Sleep(1);//so we don't use 100% cpu and only 1 so we have as little waiting times as possible.
}
}
I exit it by calling
TerminateThread(...);
Sleep(50);
and everything works fine now.
hmmmm... you're right, it does work. I wonder why the example I found used a second function outside of the threadproc.
It also seems that that sped up my app a lot.
I ended up having to use a second thread like that because I needed to be able to use the menus while the gameloop was going and, due to my poor yet improving programming skills, that is the only way I could find to do it. In essence, the secondary thread is my main thread while the primary thread just waits on the user to do something that generates a message.
In order to keep everything together, I just used a global variable. What should I have done?
superman:you haven't had the Sleep in your code therefore you was wasteing all the time in your thread doing nothing. actually setting threadpriority to above_normal gives you a lil speed boost and makes your program run more stable in terms of fps. Unfortunetly trying to keep it in background then isn't possible as everything else works terrible. Nothing to worry when working in fullscreen mode (like using GAPI)
OT: What message does wince send when your window loses focus? I can't find it anywhere.
If you are getting your input (keys + stylus) from windows messages, then those messages will be waiting for you whenever you message pump. So they aren't lost. Your screen updating should occur many times a second, so you can just process user input in between. That is plenty fast enough for user input.
I would strongly recommend not using threads for a game. I can't think of a real reason why you would need to do that, unless you want to take advantage of a multi-processor 2000/XP box.
When your main window loses focus, you get WM_ACTIVATE with a param indicating you lost activation.
Dan East
If you had focus you can process WM_SETFOCUS and WM_KILLFOCUS messages.
I would only use a thread to process some long running background operation eg. printing.
Another method is to split a long job into small parts and run one bit at a time and return control to the input processing in between.
Wow, your answers really surprised me. I know that all messages will be waiting for me to process, but processing messages less then 10 times per second creates pretty bad feeling for the user, I believe. My program uses a single thread to calculate graphic which might take anywhere from 1/100sec to several seconds and it works pretty fine from the thread. I don't really see a reason why I shouldn't use it. There's no performace penalty (at least not a noticeable one) or anything... could someone explain me exactly why using threads is?
Example why using threads might be better:
ever played a racing game when the fps dropped down to 10 or less? Often you use a method to press down/up a key to get a "semi pressed" key with non-analog devices like keyboard. But when fps rate drops too much it isn't possible as your key will be either pressed down or up for the whole 1/10 of a sec and possible much longer if you don't time your pressing well. With a routine that would check keys at least 50times a sec you could get smooth steering while graphic engine would work slow. It isn't really possible without threads , is it? On the other hand, my w2k has got 235 threads running... few more/less won't make a difference, or?
did some testing... 1000 threads using my loop with Sleep(1) use 30% of my duron 600... 10 threads will use 0.3% with 1-2 threads using as little as 0.05% and this number would be probably much lower if I would use longer Sleep... so performace problem doesn't occur when you handle even a dozen of threads... someone cares to explain me what's the problem then?
The argument against using multiple threads are:
1) Performance
2) Thread synchronization issues
The proper way to use threads on a single proc system is basically as an interrupt handler. Sockets on CE will block waiting for a connection. Putting that code that waits for the connect to return in another thread is a great use of threads because inside the Windows CE socket code, the OS puts the thread to sleep waiting for packets to come across the network. When packets arrive the OS wakes the thread and it is once more given CPU cycles to run. When the socket thread is sleeping, the OS doesn't schedules the CPU time to the other executing threads in the system. This is the proper way to use multiple threads because there is one thread excuting at full speed and all the others are sleeping waiting on an external event.
Threads are not free. Everytime a thread switch occurs, the OS must save the state of the current running thread, and swap to the state of the new thread. This takes time. This also pollutes the instruction cache. Perhaps your application isn't trying to eek out the last bit of performance running under Windows CE - mine is. If you have multiple threads in the system that are spinning, then the OS scheduler is going to switch between them when the thread's quantum is up. Back and forth, back and forth every 100ms (on CE 3.0, was 25ms. See CEGetThreadQuantum). This is terribly inefficent.
Threads can also complicate your code and make it far more difficult to debug. As soon as your thread needs to share data with other threads, you'll need to add support for synchronizing the threads so that one isn't trying to read data when the other is writing. Basically this involves blocking access to the shared data from all threads but one. When the one thread is finished accessing the data, it releases its lock and another thread is free to access. Things can get pretty nasty in trying to debug a problem where multiple threads have accessed the same data that wasn't protected properly. This typically shows up as data corruption and it seldom happens when running under the debugger. It's a timing related issue and one of the most difficult to track down.
Here's another issue that people who write multithreaded code frequently overlook. You absolutely must test your multithreaded app on a multiproc machine. The reason for this is that on a single proc machine, you will only ever have one thread executing at one time. As soon as you add another CPU, the OS can run 2 threads simultaneously. This will reveal all sorts of synchronization problems that would never have occurred on a single proc machine.
You write your apps the way you like. I'm just trying to bring up a few reasons that are well-known in the development community about writing threaded code for single proc machines. The argument about not having a responsive UI during a long rendering or drawing operation can still be handled within a single thread. You just need to break down what you render into smaller chunks that take less time, and calling PeekMessage() or GetAsyncKeyState between these to check for input events. In reality, we all do this already. You don't animate a sprite from one side of the screen to the other in one go. You break it up into a few pixel movements per frame, and check for input events in between.
I hope that helps. Also note that there are some big differences between how CE and the Desktop OSes handle threading. There are articles in MSDN about this: Look for "Windows CE 3.0: Enhanced Real-Time Features Provide Sophisticated Thread Handling".
ohh well... I guess another piece of code bites the dust and will be redone:) I guess I will make a function to process all waiting messages and call it between drawing every object to screen. That shouldn't take more then 1/10 of a sec or so... I will test it in a sec... thanks for the explanation Digby...
PS.syncronization wouldn't be rather an issue for me as I was useing global BOOl flags... needed just 2 so no big deal, it could go wrong only in extremely rare cases...