Threads
Xbox Programming & Development
Even tho the documentation tells you all you need to know about threads - I'm gonna put in a few words of advice here, as well as an example of nice use.
To make a thread like the DHCP thread I documented briefly in the tcp/ip section, simply do this :
CreateThread(NULL, 1024, (LPTHREAD_START_ROUTINE)SetUpNetwork, NULL, 0, NULL );
( 1024 = stack )
( SetUpNetwork = function pointer )
|
Other places I have used it - is a thread to check amount of diskspace free while debugging.
CreateThread(NULL, 128, (LPTHREAD_START_ROUTINE)FreeDiskSpaceThread, NULL, 0, NULL );
|
where the diskspace thread code is like :
char cfree[21];
char efree[21];
DWORD FreeDiskSpaceThread( void )
{
while( 1 )
{
ULARGE_INTEGER lpFreeBytesAvailable; // bytes available
ULARGE_INTEGER lpTotalNumberOfBytes; // bytes on disk
ULARGE_INTEGER lpTotalNumberOfFreeBytes; // free bytes on disk
GetDiskFreeSpaceEx( "c:\\",
&lpFreeBytesAvailable,&lpTotalNumberOfBytes,&lpTotalNumberOfFreeBytes);
double f = (((lpTotalNumberOfFreeBytes.HighPart) * ((MAXDWORD/1024)+1))
+ (lpTotalNumberOfFreeBytes.LowPart/1024));
sprintf( cfree, " 0 KB", f);
GetDiskFreeSpaceEx( "e:\\",
&lpFreeBytesAvailable,&lpTotalNumberOfBytes,&lpTotalNumberOfFreeBytes);
f = (((lpTotalNumberOfFreeBytes.HighPart) * ((MAXDWORD/1024)+1))
+ (lpTotalNumberOfFreeBytes.LowPart/1024));
sprintf( efree, " 0 KB", f);
Sleep(5000);
}
}
|
Every 5th second it will update a string with the diskspace avalable, and wont bother the CPU with much load meanwhile.
Now... As you see, it's not very hard.
BUT !!!
Be careful with pointers and sharing of variables. Nothing can crash harder and weirder than threads and linked lists. And it's incredible hard to debug since you don't really get to know if its a thread or the main thread you are debugging. :-)
|