Can I programmatically choose which core of a multi-core processor should my thread run on?

Or is it controlled by the operating system? I heard that the new Go language from Google has built-in functions for the programmer to become granular, or did I understand that this is wrong?

+5
source share
3 answers

For linux os, sched_setaffinity is your answer. It is supported with Linux 2.5.8.

Name

sched_setaffinity, sched_getaffinity - set and get a process processor affinity mask

#define _GNU_SOURCE
#include <sched.h>

int sched_setaffinity(  pid_t pid,
    size_t cpusetsize,
    cpu_set_t *mask);

int sched_getaffinity(  pid_t pid,
    size_t cpusetsize,
    cpu_set_t *mask);

per-thread, . gettid (2) pid. pid 0 , to getpid (2) . ( POSIX API , pthread_setaffinity_np (3) sched_setaffinity().)

+2

.

pthread_attr_setaffinity_np().

. - , .

Go, , Go , parallelism . ( "go-routines", ) . , . 1 CPU, , ... , , , .: -)

+3

: , , . arsane, sched_set_affinity(), pthread_setaffinity_np() pthreads.

, ( .)

, . , (, 32 ), == 1, 1. == 1, 2. ..

, = 1... 111 (32 ). , " 1, 2, 3,..., 32." , 2 , 30 .

, : 0... 010, " 2".

Which also explains why the maximum number of processors supported by linux is 32. (out of the box without configuration, x86, on most of the common hardware, without clustering, etc. etc.).

+2
source

All Articles