The IceTips code is wrong as is noted in the comments. It would work in a 2 processor system, which was common at the time. Today my old PC has 8 cores (16 logical), a new PC can have easily have 16 to 24 cores.
I’ll post my old code for any using <= C5.5 that want it built in. It may go a bit overboard finding a random Processor from the Mask allowed. Most code simply took #1 or #2. I think C5 will need a LIB with SetProcessAffinityMask() as it was not in Win32.LIB.
Module('WinAPI')
GetLastError(),PASCAL,LONG,DLL(1)
GetCurrentProcess(),PASCAL,LONG,DLL(1)
GetProcessAffinityMask(long hProcess, *LONG lpProcessAffinityMask, *LONG lpSystemAffinityMask),PASCAL,BOOL,DLL(1)
SetProcessAffinityMask(long hProcess, LONG lpProcessAffinityMask),PASCAL,BOOL,DLL(1)
End
!----------------------------------------
AffinityFix PROCEDURE
ProMask LONG !Affinity Process Mask
SysMask LONG !Affinity System Mask
SetProMask LONG !Affinity Process Mask I set
bRtn LONG !Bool return from function
BitCnt LONG,AUTO
BitHigh LONG,AUTO
BitCls CLASS
CountBits PROCEDURE(LONG inBitMap),LONG
RandomBit PROCEDURE(LONG inBitMap),LONG
END
CODE
bRtn = GetProcessAffinityMask(GetCurrentProcess(), ProMask, SysMask) !0=failed !http://msdn.microsoft.com/en-us/library/ms683213(VS.85).aspx
? DBg('AffinityFix','Get bRtn=' & bRtn & ' ProMask=' & ProMask & ' SysMask=' & SysMask )
IF bRtn=0
DBg('AffinityFix','Get Mask failed '& GetLastError() )
RETURN
END
IF ProMask <= 2 THEN RETURN. !1b or 10b(2) is fine, that's CPU 1 or 2
BitCnt = BitCls.CountBits(ProMask)
IF BitCnt < 2 THEN RETURN. !Only 1 processor in my affinity mask so leave it. Could have been set by ImageCfg.
BitCnt = BitCls.CountBits(SysMask)
IF BitCnt < 2 THEN RETURN. !System has only 1 processor in mask
SetProMask = BitCls.RandomBit(SysMask)
bRtn = SetProcessAffinityMask(GetCurrentProcess(),SetProMask)
DBg('AffinityFix','Set(' & SetProMask & ') bRtn=' & bRtn & ' prior ProMask=' & ProMask & ' SysMask=' & SysMask & choose(bRtn>0,'',' Err=' & GetLastError()))
bRtn = GetProcessAffinityMask(GetCurrentProcess(), ProMask, SysMask)
DBg('AffinityFix','ReGet bRtn=' & bRtn & ' now ProMask=' & ProMask & ' SysMask=' & SysMask )
RETURN
BitCls.CountBits PROCEDURE(LONG inBitMap)!,LONG
BeeCnt LONG,AUTO
bitNdx LONG,AUTO
CODE
BeeCnt = 0
LOOP bitNdx = 1 to 32
IF BAND(inBitMap,1) THEN BeeCnt += 1. !Bit is on, count it
inBitMap = BSHIFT(inBitMap,-1) !Shift Right, low bits fall off
WHILE inBitMap !Stop Loop If Mask = Zero
RETURN BeeCnt
BitCls.RandomBit PROCEDURE(LONG inBitMap)!,LONG
BeeCnt LONG,AUTO
bitOnNdx LONG,AUTO
OneBitOn LONG,AUTO
bitsFound LONG,DIM(32),AUTO !stick the bits I find on into this array
RandomBit LONG,AUTO
CODE
BeeCnt = 0
RandomBit = 0
CLEAR(bitsFound[])
OneBitOn = 1
LOOP bitOnNdx = 1 to 32
IF BAND(inBitMap,OneBitOn) THEN
BeeCnt += 1
bitsFound[BeeCnt] = OneBitOn
END
OneBitOn = BSHIFT(OneBitOn,1)
WHILE inBitMap >= OneBitOn
IF BeeCnt THEN
bitOnNdx = random(1,BeeCnt)
IF bitOnNdx THEN RandomBit = bitsFound[bitOnNdx].
END
RETURN RandomBit
This will need review and testing for systems with more than 32 processors.