• 13
• 18
• 19
• 27
• 9

# CreateThread woes - Machine rebooting. [solved]

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

## Recommended Posts

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]

##### Share on other sites
Quote:
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:
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...

##### Share on other sites
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
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

##### Share on other sites
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!

##### Share on other sites
Quote:
 Original post by Richy2kJust 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])

##### Share on other sites
Quote:
 Original post by Evil SteveHowever, 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!