0

I have written a test program to bind a thread on a CPU.Here is my test code:

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
DWORD WINAPI ThreadFunc(LPVOID pM) {
    while(1) {
        printf("Doesn't work anymore");   //If i add this printf function,the setthreadaffinitymask seems doesn't work anymore.Which u can see from two pictures i post.
    }
}
int main() 
{
    HANDLE h;
    DWORD_PTR RetFlag;
    h = CreateThread(NULL, 0, ThreadFunc, NULL, 0, NULL);
    if((RetFlag = SetThreadAffinityMask(h, 0x08)) == 0)
        printf("SetThreadAffinityMask fails\n");
    WaitForSingleObject(h, INFINITE);
    return 0;
}

CPU Rate Pic without printf()

CPU Rate Pic with printf()

And of course, the right result should be like pic one.But what's going on if i add printf() function in the thread function?Is there any trick i don't know?Thanks...

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
sj_lxd
  • 1
  • 1
  • 1
    Change the printf to `printf("")` and see what happens. – Raymond Chen Dec 09 '15 at 14:39
  • The setthreadaffinitymask works good if i use printf("").What does this mean? – sj_lxd Dec 10 '15 at 05:55
  • 1
    The SetThreadAffinityMask is working just fine. Your thread runs only on CPU 3. But the rest of the system is doing work on the other three processors to print the output the screen. – Raymond Chen Dec 10 '15 at 07:06
  • I got what you mean.I have another question about SetThreadAffinityMask.That is:Is there any WINDOWS API like SetThreadAffinityMask that can make every threads in a thread pool work on different CPUs.Of course, setthreadaffinitymask can only bind one thread on cpu,but how can i bind threads in a threadpool on different CPUS? – sj_lxd Dec 10 '15 at 07:18
  • Well,I've got a solution what i can use SetThreadAffinityMask(GetCurrentThread(), 0x00) in Threadpoolwork function.Thank you anyway! – sj_lxd Dec 10 '15 at 07:32
  • Don't change the affinity of threads that aren't yours. If you created your own thread pool, then you can mess with those. But don't mess with the threads in the default thread pool since you didn't create that thread pool. (It's also not clear why you are setting affinity in the first place.) – Raymond Chen Dec 10 '15 at 14:40
  • Well,What i was doing recently is about Parallel Computing with Multithread.So bind one thread to one CPU can reduce the consumption of CPU switch threads.That is why i wanna bind the threads of a threadpool to different CPUS. – sj_lxd Dec 10 '15 at 14:50
  • The operating system already tries to keep a thread on a single CPU. But if the kernel borrows the CPU (e.g., to handle a hardware interrupt or a kernel APC), then the operating system can move the thread to another CPU so it can keep running. Setting thread affinity means that the thread must wait for the kernel to return that CPU to user mode. You typically don't worry about context switch overhead until profiling tells you that it's a problem. And if you're doing a lot of `printf`, then context switch overhead is the least of your problems. – Raymond Chen Dec 10 '15 at 18:38

1 Answers1

1

I think that the affinity does work, but the issue here is that printf() writes some text (to the console?) and waits synchronously for the text to be written.

Now, this text has to be printed somewhere, and that somewhere is in charge of a different thread/process, it can even be real I/O to disk or network. That's why you see the drop in CPU usage of CPU3: it is idle waiting for that I/O to complete.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • Well,actually the drop of CPU3 is not the result of printf() version.But it is the end of last version(without printf()). – sj_lxd Dec 10 '15 at 05:57