首页 \ 问答 \ 争用共享变量的条件(Race condition for shared variable)

争用共享变量的条件(Race condition for shared variable)

我有一个double类型的共享变量。 这个变量将被两个线程访问。 一个线程永远只写变量,而另一个线程永远只读变量。

我仍然在这里获得比赛条件吗? 如果是的话,C ++中有没有一种“简单”的方式来实现原子访问? 如果读取数比写入数多得多,我如何有效地实现它? 我需要将变量标记为volatile吗?

编辑:确定“读取器”线程定期在批量数据上工作,并且新值的传播不是时间敏感的。 我可以声明写入器线程将写入的另一个临时变量,而不是实现复杂的互锁,而我没有很好的方法来测试。 然后,当阅读器完成一个批处理时,它可以将临时值自动传播到实际变量。 那会是无竞赛条件的吗?


I have a shared variable of type double. This variable will be accessed by two threads. One thread will ever only write the variable, whereas the other thread will ever only read the variable.

Do I still get race condition here? If yes, is there an "easy" way in C++ to implement atomic access? How do I implement it efficiently if there is going to be much much more reads than writes? Do I need to mark the variable as volatile?

EDIT: OK the "reader" thread works periodically on batches of data and the propagation of the new value is not time-sensitive. Instead of implementing complicated interlocking that I have no good way to test, I can just declare another temp variable the writer thread will write to. Then when the reader is finished with one batch, it can atomically propagate the temp value to the actual variable. Would that be race-condition-free?


原文:https://stackoverflow.com/questions/10200147
更新时间:2023-06-07 10:06

最满意答案

我编写了一些java代码来将我的HashMap转换为List而不是String [],并且ibatis使用List.size方法工作得更好。


I wrote some java code to convert my HashMap to have a List instead of String[] and ibatis worked much better with the List.size method.

相关问答

更多
  • 根据您的描述,IMHO应该使用POST。 POST用于将数据放在服务器上,在某些情况下可以获得答案。 在您的情况下,您进行搜索(向服务器发送查询)并获取该搜索的结果(检索查询结果)。 GET的定义说,它必须用于检索已经存在的资源。 根据定义,POST是创建一个新的资源。 这正是您正在做的:在服务器上创建资源并检索它! 即使您不存储搜索结果,也会在服务器上创建一个对象并将其检索。 正如PeterMmm先生所说,您可以使用POST(创建和存储查询结果)来执行此操作,然后使用GET来检索查询,但是只需要POST就 ...
  • 这个基于0的数组没有任何问题。 您正在调用getNetworkInfo的(已弃用的)重载,该重载采用整数参数。 此整数指定“您感兴趣的哪个networkType”,例如TYPE_MOBILE (0), TYPE_WIFI (1),而不是与getAllNetworks返回的数组相关的索引位置 。 你需要调用getNetworkInfo重载来获取Network作为参数,正如@nil在评论中所说的那样。 for i := 0 to networks.Length - 1 do begin network ...
  • 您可以在SQL中完成所有求和: function countLen($email) { $res = mysql_query("SELECT SUM(LENGTH(url)) as 'sum' FROM urls WHERE user='$email'"); return current(mysql_fetch_array($res)); } You could just do it all the summing in SQL: function countLen($email) { ...
  • 我会这样试试:

    相关文章

    更多
  • 解决Eclipse无法打开“Failed to load the JNI shared library”
  • Java并发编程总结---Hadoop核心源码实例解读
  • 关于不定条件写法的问题
  • Java变量类型
  • Groovy 变量详解
  • Hadoop map reduce 过程获取环境变量
  • java变量:什么是变量
  • Oracle ORA-04031 错误 说明
  • Oracle ORA-04031 错误 说明
  • Oracle ORA-04031 错误 说明

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。