Comments (10)
打印的是cycles,要除以3.5换算成ns。
from spsc_queue.
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.
用C++11的atomic吧,别用asm了
from spsc_queue.
嗯。用std::atomic正常。。
但我现在有一个值是在共享内存中创建的,这个值两个进程在读写,我想保存它的原子性。
用std::atomic好象不行,照你的例子中用内存壁垒的写法,试了试,也不行,不知道是不是我的用法不对,求指教
from spsc_queue.
atomic可以用于两个进程读写
from spsc_queue.
例如:
int nVal = pSharedMemory->nVal;
这个nVal怎么变成std::atomic?
from spsc_queue.
std::atomic<int>
,你最好先学习下c++11的atomic
from spsc_queue.
好的,谢谢,我去查查资料。
再问一下,那个asm volatile是不是只能在一行代码中保证原子性
asm volatile("" : : "m"(a) : );
a--;
asm volatile("" : : "m"(a) : );
这种三行,其实不能保证a的原子性,对吧
from spsc_queue.
https://zhuanlan.zhihu.com/p/41872203
看这篇文章
from spsc_queue.
感谢
from spsc_queue.
Related Issues (4)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spsc_queue.