how to run each thread on other core?

巧了我就是萌 提交于 2019-12-02 06:54:28

问题


I have a udp server that receive data and computing it.

I have two thread for each role.

In my cpu is a 8 multi-core and I send data in varius speed.

but at maximun I use ony %14 percent of my cpu two core 50%. if I send more data valume my buffer will fulled and don't use more cpu.

why each core arise only 50% and not more?

I think to divide this two role to multi-core.

I want to be sure that each one on other core.

how I can Explicitly to choose each thread run on other core?

my program worte on c++ visaul studio 9 and run on windows7 and I use boost::thread.


回答1:


The scheduler will deal with where your threads etc will run. This is OS specific, therefore if you want to attempt to alter how code is run you would need an OS specific API that lets you set a threads affinity etc.

Also, depends what you application is like, its a client server by the looks of it, so its not totally CPU bound. How many threads do you have in total, you mention 2 per role? A thread can only be run on one CPU. Try make units of work that can truly run in parallel, that way they can be truly run independently, ideally on different cores.

The OS will generally do a good job of running your code since it will have a better overall picture.




回答2:


You cannot make one thread use more than one core. To achieve better CPU utilization you need to redesign your program to create more threads and let the OS schedule them for you. There's no need to manually restrict the threads to specific cores. OSes are really good at figuring out how to allocate cores to threads.

In your case, if the data computing tasks are CPU heavy, you could spawn a new thread per request or have a worker thread pool that would be picking incoming tasks and processing them. This is just one of ideas. It's difficult to say without knowing more about your application architecture and the problems it's trying to solve.




回答3:


In each thread you can use SetThreadAffinityMask to choose CPUs that your thread should run on it. But I suggest you create a new worker thread for each incoming request (also if you use a thread pool you see considerable performance boost)



来源:https://stackoverflow.com/questions/12880606/how-to-run-each-thread-on-other-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!