SetThreadPriorityBoost does not work.

  • Thread starter Thread starter cppBeginnnnnner
  • Start date Start date
C

cppBeginnnnnner

Guest
Hi Guys,


I created 8 threads, 2 of them are set to THREAD_PRIORITY_ABOVE_NORMAL priority, rest of them use the default priority.

Then I run SetThreadPriorityBoost to disable the PriorityBoost for the 6 normal default threads. Then I kick of these 8 threads.

However, I still see the 6 default priority threads running before the 2 high priority threads complete..

According to the following article, 'Threads are scheduled in a round-robin fashion at each priority level, and only when there are no executable threads at a higher level does scheduling of threads at a lower level take place.'

SetThreadPriority function (processthreadsapi.h)

I have use SetProcessAffinityMask to limit all threads to 2 CPUs.

Why the 6 default priority threads are still running???

Any suggestions would be appreciated.

Here is my code.

I'm running the program in windows 10 system with 8 processors .

#include <windows.h>
#include <stdio.h>
#define THREADCOUNT 8

DWORD AccessSharedResource(LPVOID par);

int main()
{

printf("Run SetProcessAffinityMask to bind all threads to CPU0 and CPU2\n");
if(!SetProcessAffinityMask(GetCurrentProcess(),0x05))
{
DWORD dError=GetLastError();
printf("SetProcessAffinityMask failed with error:%d\n",dError);
}
HANDLE hThread[THREADCOUNT];
DWORD lpThreadId;
for(int i=0;i<THREADCOUNT;i++)
{
hThread=CreateThread(
NULL,//secuirty flag
0,//default stack size
(LPTHREAD_START_ROUTINE)AccessSharedResource, the function associated to this threa
(LPVOID)i,//the parameter passed to function AccessSharedResource
CREATE_SUSPENDED,
&lpThreadId // thread Identifier
);


if(hThread==NULL)
{
printf("Failed to create Thread, errorcode=%d",GetLastError());
return 0;
}

}


for(int i=0;i<THREADCOUNT;i++)
{


if(i>=THREADCOUNT-2)
{
if(SetThreadPriority(hThread,THREAD_PRIORITY_ABOVE_NORMAL))
{
printf("SetThreadPriority THREAD_PRIORITY_ABOVE_NORMAL to thread %d\n",i);
}
else
{
int dError=GetLastError();
printf("SetThreadPriority failed with error %d\n",dError);
}
}
else
{
if(SetThreadPriorityBoost(hThread,TRUE))//Disable Priority boost for thread 0~thread5
{
printf("SetThreadPriorityBoost disable for thread %d\n",i);
}
}

}


for(int i=0;i<THREADCOUNT;i++)
{
printf("priority of thread %d is %d\n",i,GetThreadPriority(hThread));
ResumeThread(hThread);
}

WaitForMultipleObjects(THREADCOUNT, hThread,true,INFINITE);

for(int i=0;i<THREADCOUNT;i++)
{

CloseHandle(hThread);
}


return 1;
}

DWORD AccessSharedResource(LPVOID par)
{


for(int i=0;i<10000000;i++)
{
for(int j=0;j<10000000;j++)
{
if((int)par>=6)
{
printf("Thread id=%d is running on CPU %d\n",(int)par,GetCurrentProcessorNumber());
}
else
{
printf("..............You should not see this until Thread 6&7 complete....................\n");
}
}
}
if((int)par>=6)
{
printf("Thread %d has compelted\n",(int)par);
}

return 0;
}

Continue reading...
 

Similar threads

Back
Top