我最近在我的项目上运行 valgrind --tool=helgrind 并收到警告“可能的数据竞争”,我认为这是令人担忧的。然而,即使是这个简单的测试程序也会导致此消息:
#include <iostream>
#include <thread>
#include <future>
int main()
{
std::packaged_task<void()> task([]()
{
std::cout << "Hello\n"; // You can leave this out
});
auto future = task.get_future();
std::thread(std::move(task)).detach();
future.get();
}
用g++-4.9 -p -g -std=c++11 -pthread -O3 test.cpp编译,valgrind --tool=helgrind ./a的输出。出是:
==12808== Helgrind, a thread error detector
==12808== Copyright (C) 2007-2013, and GNU GPL'd, by OpenWorks LLP et al.
==12808== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==12808== Command: ./a.out
==12808==
Hello
==12808== ---Thread-Announcement------------------------------------------
==12808==
==12808== Thread #2 was created
==12808== at 0x567B73E: clone (clone.S:74)
==12808== by 0x536A1F9: do_clone.constprop.4 (createthread.c:75)
==12808== by 0x536B7EB: create_thread (createthread.c:245)
==12808== by 0x536B7EB: pthread_create@@GLIBC_2.2.5 (pthread_create.c:606)
==12808== by 0x4C30E3D: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==12808== by 0x4EF7F08: std::thread::_M_start_thread(std::shared_ptr<std::thread::_Impl_base>) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.20)
==12808== by 0x403C3D: std::thread::thread<std::packaged_task<void ()>>(std::packaged_task<void ()>&&) (thread:138)
==12808== by 0x401D57: main (test.cpp:13)
==12808==
==12808== ----------------------------------------------------------------
==12808==
==12808== Thread #2: pthread_cond_{signal,broadcast}: dubious: associated lock is not held by any thread
==12808== at 0x4C2F295: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==12808== by 0x4EF3CE8: std::condition_variable::notify_all() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.20)
==12808== by 0x402F18: _M_set_result (future:374)
==12808== by 0x402F18: _M_run (future:1319)
==12808== by 0x402F18: operator() (future:1453)
==12808== by 0x402F18: _M_invoke<> (functional:1700)
==12808== by 0x402F18: operator() (functional:1688)
==12808== by 0x402F18: std::thread::_Impl<std::_Bind_simple<std::packaged_task<void ()> ()> >::_M_run() (thread:115)
==12808== by 0x4EF7DCF: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.20)
==12808== by 0x4C30FD6: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==12808== by 0x536B0A4: start_thread (pthread_create.c:309)
==12808== by 0x567B77C: clone (clone.S:111)
==12808==
==12808== ---Thread-Announcement------------------------------------------
==12808==
==12808== Thread #1 is the program's root thread
==12808==
==12808== ----------------------------------------------------------------
==12808==
==12808== Possible data race during read of size 8 at 0x5C4FAA0 by thread #1
==12808== Locks held: none
==12808== at 0x4026A9: ~unique_ptr (unique_ptr.h:235)
==12808== by 0x4026A9: ~_Task_state_base (future:1281)
==12808== by 0x4026A9: ~_Task_state (future:1302)
==12808== by 0x4026A9: destroy<std::__future_base::_Task_state<main()::<lambda()>, std::allocator<int>, void()> > (new_allocator.h:124)
==12808== by 0x4026A9: _S_destroy<std::__future_base::_Task_state<main()::<lambda()>, std::allocator<int>, void()> > (alloc_traits.h:282)
==12808== by 0x4026A9: destroy<std::__future_base::_Task_state<main()::<lambda()>, std::allocator<int>, void()> > (alloc_traits.h:411)
==12808== by 0x4026A9: std::_Sp_counted_ptr_inplace<std::__future_base::_Task_state<main::{lambda()#1}, std::allocator<int>, void ()>, main::{lambda()#1}, (__gnu_cxx::_Lock_policy)2>::_M_dispose() (shared_ptr_base.h:524)
==12808== by 0x401ED4: _M_release (shared_ptr_base.h:149)
==12808== by 0x401ED4: ~__shared_count (shared_ptr_base.h:666)
==12808== by 0x401ED4: ~__shared_ptr (shared_ptr_base.h:914)
==12808== by 0x401ED4: reset (shared_ptr_base.h:1015)
==12808== by 0x401ED4: ~_Reset (future:657)
==12808== by 0x401ED4: get (future:786)
==12808== by 0x401ED4: main (test.cpp:14)
==12808==
==12808== This conflicts with a previous write of size 8 by thread #2
==12808== Locks held: none
==12808== at 0x403407: release (unique_ptr.h:328)
==12808== by 0x403407: unique_ptr<std::__future_base::_Result<void>, std::__future_base::_Result_base::_Deleter, void> (unique_ptr.h:221)
==12808== by 0x403407: ~packaged_task (future:1409)
==12808== by 0x403407: ~_Head_base (tuple:128)
==12808== by 0x403407: ~_Tuple_impl (tuple:229)
==12808== by 0x403407: ~tuple (tuple:388)
==12808== by 0x403407: ~_Bind_simple (functional:1663)
==12808== by 0x403407: std::thread::_Impl<std::_Bind_simple<std::packaged_task<void ()> ()> >::~_Impl() (thread:107)
==12808== by 0x4EF7E1A: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.20)
==12808== by 0x4C30FD6: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==12808== by 0x536B0A4: start_thread (pthread_create.c:309)
==12808== by 0x567B77C: clone (clone.S:111)
==12808== Address 0x5c4faa0 is 128 bytes inside a block of size 144 alloc'd
==12808== at 0x4C2C520: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==12808== by 0x401C8C: allocate (new_allocator.h:104)
==12808== by 0x401C8C: allocate (alloc_traits.h:357)
==12808== by 0x401C8C: __shared_count<std::__future_base::_Task_state<main()::<lambda()>, std::allocator<int>, void()>, std::allocator<int>, main()::<lambda()>, const std::allocator<int>&> (shared_ptr_base.h:616)
==12808== by 0x401C8C: __shared_ptr<std::allocator<int>, main()::<lambda()>, const std::allocator<int>&> (shared_ptr_base.h:1090)
==12808== by 0x401C8C: shared_ptr<std::allocator<int>, main()::<lambda()>, const std::allocator<int>&> (shared_ptr.h:316)
==12808== by 0x401C8C: allocate_shared<std::__future_base::_Task_state<main()::<lambda()>, std::allocator<int>, void()>, std::allocator<int>, main()::<lambda()>, const std::allocator<int>&> (shared_ptr.h:588)
==12808== by 0x401C8C: __create_task_state<void(), main()::<lambda()>, std::allocator<int> > (future:1351)
==12808== by 0x401C8C: packaged_task<main()::<lambda()>, std::allocator<int>, void> (future:1403)
==12808== by 0x401C8C: packaged_task<main()::<lambda()>, void> (future:1393)
==12808== by 0x401C8C: main (test.cpp:11)
==12808== Block was alloc'd by thread #1
==12808==
==12808== ----------------------------------------------------------------
==12808==
==12808== Possible data race during read of size 1 at 0x5C4FA6C by thread #1
==12808== Locks held: none
==12808== at 0x4C2ED90: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==12808== by 0x4C2F57F: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==12808== by 0x4026D7: ~_State_baseV2 (future:316)
==12808== by 0x4026D7: ~_Task_state_base (future:1281)
==12808== by 0x4026D7: ~_Task_state (future:1302)
==12808== by 0x4026D7: destroy<std::__future_base::_Task_state<main()::<lambda()>, std::allocator<int>, void()> > (new_allocator.h:124)
==12808== by 0x4026D7: _S_destroy<std::__future_base::_Task_state<main()::<lambda()>, std::allocator<int>, void()> > (alloc_traits.h:282)
==12808== by 0x4026D7: destroy<std::__future_base::_Task_state<main()::<lambda()>, std::allocator<int>, void()> > (alloc_traits.h:411)
==12808== by 0x4026D7: std::_Sp_counted_ptr_inplace<std::__future_base::_Task_state<main::{lambda()#1}, std::allocator<int>, void ()>, main::{lambda()#1}, (__gnu_cxx::_Lock_policy)2>::_M_dispose() (shared_ptr_base.h:524)
==12808== by 0x401ED4: _M_release (shared_ptr_base.h:149)
==12808== by 0x401ED4: ~__shared_count (shared_ptr_base.h:666)
==12808== by 0x401ED4: ~__shared_ptr (shared_ptr_base.h:914)
==12808== by 0x401ED4: reset (shared_ptr_base.h:1015)
==12808== by 0x401ED4: ~_Reset (future:657)
==12808== by 0x401ED4: get (future:786)
==12808== by 0x401ED4: main (test.cpp:14)
==12808==
==12808== This conflicts with a previous write of size 4 by thread #2
==12808== Locks held: none
==12808== at 0x536F9BF: pthread_cond_broadcast@@GLIBC_2.3.2 (pthread_cond_broadcast.S:59)
==12808== by 0x402F18: _M_set_result (future:374)
==12808== by 0x402F18: _M_run (future:1319)
==12808== by 0x402F18: operator() (future:1453)
==12808== by 0x402F18: _M_invoke<> (functional:1700)
==12808== by 0x402F18: operator() (functional:1688)
==12808== by 0x402F18: std::thread::_Impl<std::_Bind_simple<std::packaged_task<void ()> ()> >::_M_run() (thread:115)
==12808== Address 0x5c4fa6c is 76 bytes inside a block of size 144 alloc'd
==12808== at 0x4C2C520: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==12808== by 0x401C8C: allocate (new_allocator.h:104)
==12808== by 0x401C8C: allocate (alloc_traits.h:357)
==12808== by 0x401C8C: __shared_count<std::__future_base::_Task_state<main()::<lambda()>, std::allocator<int>, void()>, std::allocator<int>, main()::<lambda()>, const std::allocator<int>&> (shared_ptr_base.h:616)
==12808== by 0x401C8C: __shared_ptr<std::allocator<int>, main()::<lambda()>, const std::allocator<int>&> (shared_ptr_base.h:1090)
==12808== by 0x401C8C: shared_ptr<std::allocator<int>, main()::<lambda()>, const std::allocator<int>&> (shared_ptr.h:316)
==12808== by 0x401C8C: allocate_shared<std::__future_base::_Task_state<main()::<lambda()>, std::allocator<int>, void()>, std::allocator<int>, main()::<lambda()>, const std::allocator<int>&> (shared_ptr.h:588)
==12808== by 0x401C8C: __create_task_state<void(), main()::<lambda()>, std::allocator<int> > (future:1351)
==12808== by 0x401C8C: packaged_task<main()::<lambda()>, std::allocator<int>, void> (future:1403)
==12808== by 0x401C8C: packaged_task<main()::<lambda()>, void> (future:1393)
==12808== by 0x401C8C: main (test.cpp:11)
==12808== Block was alloc'd by thread #1
==12808==
==12808==
==12808== For counts of detected and suppressed errors, rerun with: -v
==12808== Use --history-level=approx or =none to gain increased speed, at
==12808== the cost of reduced accuracy of conflicting-access information
==12808== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 23 from 23)
我的问题:忽略它是否安全?如果是这样,如何抑制这些警告?
最佳答案
我也遇到了这个警告,并在 Kubuntu 14.04 上检查了 GCC 4.8 ( /usr/include/c++/4.8/future ) 的 libstdc++ 源代码。我没有发现错误。 future的状态包含结果指针、互斥量和条件变量等。 future<T>::wait()锁定互斥量并检查结果指针是否为空。如果是,它将等待条件变量(这也将在一个原子操作中解锁互斥量)。在 promise<T>::set_value() (或者我猜是 set_exception() ),代码锁定互斥锁,设置结果指针,解锁互斥锁,然后调用 notify_all()在 condition variable 上.因此 Helgrind 发出警告,因为当 notify_all() 时互斥体未被锁定。叫做。然而,代码仍然是安全的,因为互斥量是用于设置指针的,这是更改 future<T>::wait() 中检查结果的唯一方法。 .在 lock_guard 的生命周期之外进行通知可能是一种优化(尽可能短暂地持有互斥量)。不幸的是,它确实导致了这个警告。 libstdc++ 维护者可能不愿意接受补丁,因为 a) 补丁会降低性能,如果是轻微的话,并且 b) 如果它没有损坏就不要修复它。因此,我的建议是抑制 Helgrind。
请注意,为了清晰和简洁,我简化了一些交互,特别是 future和 promise将实际工作委托(delegate)给隐藏的实现类,set_value使用额外的 once_flag同步并发调用等。
另请参阅此主题、我对此的回答以及 Cubbi 对我的回答的准确评论(我忽略了这一点): Why do both the notify and wait function of a std::condition_variable need a locked mutex
关于c++ - 使用 packaged_task 和线程的可能数据竞争,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27424084/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po