how to run each thread on other core?

2019-03-02 08:56发布

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.

3条回答
趁早两清
2楼-- · 2019-03-02 09:54

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)

查看更多
Fickle 薄情
3楼-- · 2019-03-02 09:57

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.

查看更多
三岁会撩人
4楼-- · 2019-03-02 10:01

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.

查看更多
登录 后发表回答