摘录


清单 2. 为每个线程设置 CPU 亲和性(affinity)
   cpu_set_t mask;
   / CPU_ZERO initializes all the bits in the mask to zero. /
        CPU_ZERO( &mask );
   / CPU_SET sets only the bit corresponding to cpu. /
        CPU_SET( created_thread, &mask );
   / sched_setaffinity returns 0 in success /
        if( sched_setaffinity( 0, sizeof(mask), &mask ) == -1 )
   {
      printf("WARNING: Could not set CPU Affinity, continuing...\n");
   }
如果程序可以执行到这儿,那么我们的线程就已经设置了自己的亲和性(affinity)。调用 sched_setaffinity 会设置由 pid 所引用的进程的 CPU 亲和性(affinity)掩码。如果 pid 为 0,那么就使用当前进程。

点评

除了这种方法,还可以使用taskset工具来管理。

原文

点击这里查看原文

其它

本帖内容由21QA云收藏工具自动生成,欢迎使用。

系统消息 若觉得内容不错,请点击左上角的"赞"图标,以优化网站的内容呈现。 另外,请及时验证注册邮箱,否则收不到21QA发出的红包。 官方Q群:250203055

asked 12 Jan '15, 10:04

%E5%8E%9F%E9%87%8E%E4%B9%8B%E7%8B%BC's gravatar image

原野之狼
1.9k9399116

Be the first one to answer this question!
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link: [text](http://url.com/ "title")
  • image: ![alt](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×856
×323

question asked: 12 Jan '15, 10:04

question was seen: 1,015 times

last updated: 12 Jan '15, 10:04

powered by O*S*Q*A

粤ICP备14040061号-1