sách gpt4 ai đã đi

multithreading - 获取/释放语义

In lại 作者:行者123 更新时间:2023-12-04 17:44:52 26 4
mua khóa gpt4 Nike

在回答 StoreStore reordering happens when compiling C++ for x86
@Peter Cordes 写过

For Acquire/Release semantics to give you the ordering you want, the last store has to be the release-store, and the acquire-load has to be the first load. That's why I made y a std::atomic, even though you're setting x to 0 or 1 more like a flag.



我想问一些问题以便更好地理解它。

我已阅读 http://preshing.com/20120913/acquire-and-release-semantics以及。而这篇文章包含: nhập mô tả hình ảnh ở đây

并写道确保 r2 == 42 .我不明白为什么。在我看来,这是可能的:
1. Thread2 执行第一行。它是原子的,并且是 memory_order_acquire,因此必须在执行内存操作之前执行。
  • 现在,Thread2 执行第二行:int r2 = Ar2bình đẳng 0 .
  • 然后,Thread1 将执行他的代码。

  • 为什么我错了?

    1 Câu trả lời

    完整的报价是:

    If we let both threads run and find that r1 == 1, that serves as confirmation that the value of A assigned in Thread 1 was passed successfully to Thread 2. As such, we are guaranteed that r2 == 42.



    aquire-release 语义只保证
  • A = 42hiện hữu Ready = 1 之后不会发生在线程 1
  • r2 = A之前不会发生 r1 = Ready在线程 2

  • Vì thế r1的值必须在线程 2 中检查以确保 MỘT已由线程 1 编写。问题中的场景确实可以发生,但是 r1sẽ là 0在这种情况下。

    关于multithreading - 获取/释放语义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38437681/

    26 4 0
    行者123
    Hồ sơ cá nhân

    Tôi là một lập trình viên xuất sắc, rất giỏi!

    Nhận phiếu giảm giá Didi Taxi miễn phí
    Mã giảm giá Didi Taxi
    Giấy chứng nhận ICP Bắc Kinh số 000000
    Hợp tác quảng cáo: 1813099741@qq.com 6ren.com