Use, SetThreadPriority
Of course this will only affect your thread. Here is an example:
#include <windows.h>#include <process.h>#define NUM_THREADS 1LONGLONG sum;unsigned __stdcall CalcThread( void *arg ) { for( long i = 0; i < 100000000; i++ ) { sum += 2; } printf("finished thread: %u\n", (long) arg); return 0;}int main() { HANDLE hthread[NUM_THREADS]; // handles for the threads. unsigned ThreadID; // Receives each thread's ID. hthread[0] = (HANDLE) _beginthreadex( NULL, 0, CalcThread, (void *) GetCurrentThreadId(), CREATE_SUSPENDED, &ThreadID ); if( !SetThreadPriority( hthread[0], THREAD_PRIORITY_HIGHEST ) ) { printf("Priority Set Error\n"); return 0; } Sleep( 5000 ); if( ResumeThread( hthread[0] ) == 0xFFFFFFFF ) { printf("Resuming Thread Error\n"); return 0; } Sleep( 1000 ); if( WaitForMultipleObjects( NUM_THREADS, hthread, TRUE, INFINITE ) == WAIT_FAILED ) { printf("WaitForMultipleObjects failed\n"); } printf("sum = %u\n", (long)sum); return 0;}
That should be all to it. Let me know if you run into any problems. Also be sure to do this:
Go to the project settings, choose the C++ tab and pick "Code Generation" from the ComboBox. Then, set "Use Runtime Library" to the appropriate multithreaded version, I'd recommend 'Multithreaded DLL'.
Edit: Updated source code. If you need a file called MSVCIRTD.dll, you can find it here.
Just scroll down till you find it. Save it to your Windows\System if you are using Win98 or WinME, or Windows\System32 with Win2000 or higher
Example for _beginthreadex. Check the bottom of that page, the example looks similar to the main code above.
Hope this helps,
- [BDS]StackOverflow
[edited by - BlueDev on March 19, 2004 2:00:05 PM]