How do the likely/unlikely macros in the Linux kernel work and what is their benefit?

后端 未结 10 711
攒了一身酷
攒了一身酷 2020-11-22 12:58

I\'ve been digging through some parts of the Linux kernel, and found calls like this:

if (unlikely(fd < 0))
{
    /* Do something */
}

o

10条回答
  •  隐瞒了意图╮
    2020-11-22 13:11

    In many linux release, you can find complier.h in /usr/linux/ , you can include it for use simply. And another opinion, unlikely() is more useful rather than likely(), because

    if ( likely( ... ) ) {
         doSomething();
    }
    

    it can be optimized as well in many compiler.

    And by the way, if you want to observe the detail behavior of the code, you can do simply as follow:

    gcc -c test.c objdump -d test.o > obj.s

    Then, open obj.s, you can find the answer.

提交回复
热议问题