gpt4 book ai didi

c++ - test_and_set_bit 它是如何工作的?

转载 作者:行者123 更新时间:2023-12-04 07:22:08 24 4
gpt4 key购买 nike

有人可以根据接受的答案回答我的问题吗?
我正在阅读:https://www.kernel.org/doc/htmldocs/kernel-api/API-test-and-set-bit.html但没明白那么多。

  • 如果该位已经是 1,它是保持 1 还是 0?
  • 如果它是 0 并且我没有错,那么它会更改为 1?
  • 这里的“位”是什么,输入可以是 bool 值,它是字节而不是位...
  • 返回值是否意味着在更改值后我们又变回原来的状态?如果是这样,那么这是无用的......
  • 在 C++ 中,当我尝试调用它时,我得到:
    使用未声明的标识符“test_and_set_bit”

  • 抱歉,该页面根本不清楚。

    用法:
    typedef struct lock {
    bool is_locked;
    } lock_t;

    void init(lock_t* l) {
    l->is_locked = 0;
    }

    void lock(lock_t* l) {
    while (test_and_set_bit
    (l->is_locked));
    }

    void unlock(lock_t* l) {
    l->is_locked = 0;
    }

    最佳答案

    If the bit was already 1 does it stay 1 or 0?


    它将被设置为 1

    If it's 0 and I'm not wrong then it changes to 1?


    是的,它更改为 1

    What is "bit" here, an input can be bool which is byte not bit...


    位是一位。不是一个字节。输入不能为 bool。例如,如果您有一个 unsigned char它由 8 位组成

    By return do they mean that after changing the value we change in back to its original condition? if so then this is useless...


    否。该函数将返回该位之前的值。然后该函数将该位设置为 1

    In C++ when I try to call it I get: Use of undeclared identifier 'test_and_set_bit'


    在标准 C++ 中,此函数不可用。它是一个在外部库中定义的函数。

    关于c++ - test_and_set_bit 它是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68428461/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com