Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
There are two CPU in my computer, named CPU0 and CPU1.
First,I created 12 thread,and I want this program's every thread to run at CPU0, but after I used the fuction setthreadaffinitymask, it does not work. Is there anything wrong with the program?

C++
#include <windows.h>
#include <stdio.h>
#define MAX_SEM_COUNT 10
#define THREADCOUNT 12
HANDLE ghSemaphore;
DWORD WINAPI ThreadProc( LPVOID );
void main()
{
    HANDLE aThread[THREADCOUNT];
    DWORD ThreadID;
    int i;
    // Create a semaphore with initial and max counts of MAX_SEM_COUNT
    ghSemaphore = CreateSemaphore( 
        NULL,           // default security attributes
        MAX_SEM_COUNT,  // initial count
        MAX_SEM_COUNT,  // maximum count
        NULL);          // unnamed semaphore
    if (ghSemaphore == NULL) 
    {
        printf("CreateSemaphore error: %d\n", GetLastError());
        return;
    }
    // Create worker threads
    for( i=0; i < THREADCOUNT; i++ )
    {
        aThread[i] = CreateThread( 
                     NULL,       // default security attributes
                     0,          // default stack size
                     (LPTHREAD_START_ROUTINE) ThreadProc, 
                     NULL,       // no thread function arguments
                     CREATE_SUSPENDED,          // default creation flags
                     &ThreadID); // receive thread identifier
		
        if( aThread[i] == NULL )
        {
            printf("CreateThread error: %d\n", GetLastError());
            return;
        }
		SetThreadAffinityMask(aThread[i],0x00000001);
		ResumeThread(aThread[i]);		
    }	
    // Wait for all threads to terminate
	
    WaitForMultipleObjects(THREADCOUNT, aThread, TRUE, INFINITE);
	
    // Close thread and semaphore handles
	
    for( i=0; i < THREADCOUNT; i++ )
        CloseHandle(aThread[i]);
	
    CloseHandle(ghSemaphore);
	system("pause");
}
DWORD WINAPI ThreadProc( LPVOID lpParam )
{
//	SetThreadAffinityMask(GetCurrentThread(),0x1);
    DWORD dwWaitResult; 
    BOOL bContinue=TRUE;
	
    while(bContinue)
    {
        // Try to enter the semaphore gate.
		
        dwWaitResult = WaitForSingleObject( 
            ghSemaphore,   // handle to semaphore
            0L);           // zero-second time-out interval
		
        switch (dwWaitResult) 
        { 
            // The semaphore object was signaled.
		case WAIT_OBJECT_0: 
			// TODO: Perform task
			printf("Thread %d: wait succeeded\n", GetCurrentThreadId());
			bContinue=FALSE;            
			
			// Simulate thread spending time on task
			Sleep(5);
			
			// Relase the semaphore when task is finished
			
			if (!ReleaseSemaphore( 
				ghSemaphore,  // handle to semaphore
				1,            // increase count by one
				NULL) )       // not interested in previous count
			{
				printf("ReleaseSemaphore error: %d\n", GetLastError());
			}
			break; 
			
            // The semaphore was nonsignaled, so a time-out occurred.
		case WAIT_TIMEOUT: 
			printf("Thread %d: wait timed out\n", GetCurrentThreadId());
			break; 
        }
    }
    return TRUE;
}
Posted
Updated 29-Dec-10 19:41pm
v2

1 solution

you need to make sure that the parent process does not denies the thread from executing the specified processor.

From msdn

"A thread affinity mask must be a subset of the process affinity mask for the containing process of a thread. A thread can only run on the processors its process can run on. Therefore, the thread affinity mask cannot specify a 1 bit for a processor when the process affinity mask specifies a 0 bit for that processor."

see SetProcessAffinityMask also

good luck
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900