CreateThread woes - Machine rebooting. [solved]

Started by
4 comments, last by Richy2k 17 years, 4 months ago
Just been getting ogg streaming to work with or without multithreading in my engine, and came across a nasty little problem: CreateThread appears to reboot my machine after about 5 or 6 calls. When it does not reboot my machine, it functions correctly. I'll post the line of code I am using to spawn the thread even though I doubt its my calling of it:

m_ThreadHandle = CreateThread( 0, 0, (LPTHREAD_START_ROUTINE)&cThread::entryPoint, static_cast< void* >( this ), 0, &m_ThreadID);
Data types are correct. The function that is being ran in this thread is just a while loop which bails when there is no more data to stream, or it is signalled to quit. It is never terminated manuelly, it is let to die. Problem is, it works fine on my work machine, works perfectly every time. Yet to try the same code recompiled for Linux (using pthreads) to see if the problem lies elsewhere, but I believe that will work correctly, on a different machine to the problematic one. Has anyone else experienced any similar problems to this? I think it is my PC being a little foobar, but I was wondering if anyone else has had similar things happen to them? If so, please share any solutions [smile] Not sure if this was the right forum, but it is kinda half programming half hardware problem.. Thanks. [Edited by - Richy2k on December 11, 2006 4:04:20 PM]
Adventures of a Pro & Hobby Games Programmer - http://neilo-gd.blogspot.com/Twitter - http://twitter.com/neilogd
Advertisement
Quote:Original post by Richy2k
m_ThreadHandle = CreateThread( 0, 0, (LPTHREAD_START_ROUTINE)&cThread::entryPoint, static_cast< void* >( this ), 0, &m_ThreadID);
Never, never, never cast function pointers like that. If this code doesn't compile, then it means that your stack is likely to be screwed up if it works when casting:
m_ThreadHandle = CreateThread( 0, 0, &cThread::entryPoint, this, 0, &m_ThreadID); // Shouldn't need to cast this to a void* either
cThread::entryPoint is a static function, isn't it?

As for the rebooting, Goto Control Pannel -> System -> Advanced tab -> Startup and Recovery Settings -> and uncheck "Automatically Restart". That'll cause XP to give you a blue screen of death instead of rebooting, and you can write down the STOP code and google it to find out what it means, and that should hint at what's cuaing the problem.

It's likely to be dodgey hardware and/or drivers though, since it takes a fair bit of effort to bring XP to it's knees. Assuming it's XP of course...
1. You should never have to cast to that function pointer. If your type doesn't match that signature EXACTLY, then you have done something extremely wrong.

Mind you, create thread has SOME ability to detect malformed function signatures and correct for them, but intentionally abusing that ability is just like sticking a fork into your power supply.

The signature has to match this: DWORD WINAPI ThreadProc(LPVOID lpParameter);

2. Member function pointers that you want to use that way have to be to static members.

Examples:
DWORD ThreadProc(void* p); <--- NOT CORRECT
void ThreadProc(void*); <--- NOT CORRECT
struct T { DWORD ThreadProc(void* p); }; <--- NO CAN USE!
struct T { static DWORD ThreadProc(void* p); }; <--- NO CAN USE!
struct T { static DWORD WINAPI ThreadProc(void* p); }; <--- CAN USE!
DWORD WINAPI ThreadProc(void* p); <--- CORRECT
DWORD WINAPI f(LPVOID p); <--- CORRECT

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Just done a bit more reading...my static function IS incorrect. if anything it should be returning an int, not a void*...do not ask. I wrote the thread code a long time ago, and its only now it is failing. Anyways i'll try everything you said, and see what happens. I feel so dumb right now...Although its what I get for writing code for both windows and linux, and hoping itll just 'work'.

Thanks guys, its working solid now! Although I don't get how i've been using the same code quite a few times in several applications...Anyways it is working now, so time to sit back, and be happy!
Adventures of a Pro & Hobby Games Programmer - http://neilo-gd.blogspot.com/Twitter - http://twitter.com/neilogd
Quote:Original post by Richy2k
Just done a bit more reading...my static function IS incorrect. if anything it should be returning an int, not a void*...do not ask. I wrote the thread code a long time ago, and its only now it is failing. Anyways i'll try everything you said, and see what happens. I feel so dumb right now...Although its what I get for writing code for both windows and linux, and hoping itll just 'work'.
Actually that shouldn't cause any problems under the hood, since the size of an int is probably the size of a pointer.

However, I'd still try the control pannel thingy I mentioned, particularly if it's only just started happening - could be dodgey hardware (Or a bunch of other things, don't panic [smile])
Quote:Original post by Evil Steve
However, I'd still try the control pannel thingy I mentioned, particularly if it's only just started happening - could be dodgey hardware (Or a bunch of other things, don't panic [smile])


This is the first time i've abused it by trying to start the thread several times repeatedly [smile]. I'm glad this bug reared its ugly head now, it definatly works now. But as always, testing constantly!
Adventures of a Pro & Hobby Games Programmer - http://neilo-gd.blogspot.com/Twitter - http://twitter.com/neilogd

This topic is closed to new replies.

Advertisement