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

后端 未结 10 727
攒了一身酷
攒了一身酷 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:04

    (general comment - other answers cover the details)

    There's no reason that you should lose portability by using them.

    You always have the option of creating a simple nil-effect "inline" or macro that will allow you to compile on other platforms with other compilers.

    You just won't get the benefit of the optimization if you're on other platforms.

提交回复
热议问题