我在业余时间写一个电子游戏,在介绍多线程时,我有一个关于数据一致性的问题。
目前,我的游戏是单线程的,并且有一个简单的游戏循环,正如在许多教程中所教的那样:
while game window is not closed
{
poll user input
react to user input
update game state
render game objects
flip buffers
}
我现在想给我的游戏添加一个新的功能,玩家可以自动完成某些长而繁琐的任务,比如长途行走(快速旅行)。我可以选择简单地“传送”玩家的角色到他们的目的地,但我不喜
众所周知,std::atomic和volatile是两个不同的东西。
主要有两点不同:
两个优化可以用于std::atomic<int> a;,但不能用于volatile int a;
- fused operations: `a = 1; a = 2;` can be replaced by the compiler on `a = 2;`
- constant propagation: `a = 1; local = a;` can be replaced by the compiler on`a = 1; local = 1;`
跨原子/易失性操作的普通读/写的重新排序:
我正在通过短信与我的用户互动,如果他们用这个模式发送给我一条短信,我需要执行一个操作:
模式:
*TEXT*TEXT*TEXT#
在TEXT中,所有字符都是允许的,所以我做了这个正则表达式:
if (preg_match('/^\*([^*]*)\*([^*]*)\*([^#]*)\#$/', $text)){
// perform the action...
}
上面的正则表达式实际上很好,但是它不允许下一个lines after #,例如:
'*hello there*how you doing!?* and blah#'传递正则表达式,但是: