Giter VIP home page Giter VIP logo

Comments (10)

MengRao avatar MengRao commented on June 8, 2024

打印的是cycles,要除以3.5换算成ns。

from spsc_queue.

robinzhang77 avatar robinzhang77 commented on June 8, 2024

int a = 0;

void test1()
{
for (int i = 0; i < 1000; i++)
{
asm volatile("" : : "m"(a) :);
a++;
asm volatile("" : : "m"(a) :);
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}

void test2()
{
for (int i = 0; i < 1000; i++)
{
asm volatile("" : : "m"(a) : );
a--;
asm volatile("" : : "m"(a) : );

    std::this_thread::sleep_for(std::chrono::milliseconds(1));
}

}

两个线程跑。最后穿上a值不是0.。。是不是asm用错了?请教

from spsc_queue.

MengRao avatar MengRao commented on June 8, 2024

用C++11的atomic吧,别用asm了

from spsc_queue.

robinzhang77 avatar robinzhang77 commented on June 8, 2024

嗯。用std::atomic正常。。

但我现在有一个值是在共享内存中创建的,这个值两个进程在读写,我想保存它的原子性。
用std::atomic好象不行,照你的例子中用内存壁垒的写法,试了试,也不行,不知道是不是我的用法不对,求指教

from spsc_queue.

MengRao avatar MengRao commented on June 8, 2024

atomic可以用于两个进程读写

from spsc_queue.

robinzhang77 avatar robinzhang77 commented on June 8, 2024

例如:
int nVal = pSharedMemory->nVal;
这个nVal怎么变成std::atomic?

from spsc_queue.

MengRao avatar MengRao commented on June 8, 2024

std::atomic<int>,你最好先学习下c++11的atomic

from spsc_queue.

robinzhang77 avatar robinzhang77 commented on June 8, 2024

好的,谢谢,我去查查资料。
再问一下,那个asm volatile是不是只能在一行代码中保证原子性

asm volatile("" : : "m"(a) : );
a--;
asm volatile("" : : "m"(a) : );

这种三行,其实不能保证a的原子性,对吧

from spsc_queue.

MengRao avatar MengRao commented on June 8, 2024

https://zhuanlan.zhihu.com/p/41872203
看这篇文章

from spsc_queue.

robinzhang77 avatar robinzhang77 commented on June 8, 2024

感谢

from spsc_queue.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.