- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用具有工作窃取功能的线程池,但每当程序试图锁定工作队列中的互斥量时,我都会收到异常错误。
我已经在 Windows Visual Studio 2015 和 Ubuntu 14.04 上尝试过该程序,但都产生了运行时异常。
我已经对工作队列本身进行了广泛的测试,但无法重现错误。如果我注释掉 try_steal 函数,我不会遇到任何错误。最后,我用 std::recursive_mutex 替换了 std::mutex ,但我仍然遇到同样的错误。
我以为是在线程池解构过程中发生异常,即一个线程试图读取另一个已经被销毁的线程的工作队列。但是即使在程序结束之前引入了一个无限循环之后,还是出现了同样的异常。
我想知道是否还有其他我不想检查的东西,您将在下面找到相关代码以及 VS 2015 和 Linux 调用堆栈。
感谢您的帮助。
Windows 调用堆栈:
msvcp140d.dll!mtx_do_lock(_Mtx_internal_imp_t * mtx, const xtime * target) msvcp140d.dll!_Mtx_lock(_Mtx_internal_imp_t * mtx) thread_pool_test.exe!std::_Mtx_lockX(_Mtx_internal_imp_t * _Mtx) thread_pool_test.exe!std::_Mutex_base::lock() thread_pool_test.exe!std::lock_guard::lock_guard(std::mutex & _Mtx) thread_pool_test.exe!work_stealing_queue::try_steal(function_wrapper & res) thread_pool_test.exe!thread_pool_steal::pop_task_from_other_thread_queue(function_wrapper & task) thread_pool_test.exe!thread_pool_steal::run_pending_task() thread_pool_test.exe!thread_pool_steal::worker_thread(unsigned int my_index_) thread_pool_test.exe!std::_Invoker_pmf_pointer::_Call(void (unsigned int) * _Pmf, thread_pool_steal * && _Arg1, int && <_Args2_0>) Line 1373 C++ thread_pool_test.exe!std::invoke(void (unsigned int) * && _Obj, thread_pool_steal * && <_Args_0>, int && <_Args_1>) thread_pool_test.exe!std::_LaunchPad,std::default_delete > > >::_Execute<0,1,2>(std::tuple & _Tup, std::integer_sequence __formal) thread_pool_test.exe!std::_LaunchPad,std::default_delete > > >::_Run(std::_LaunchPad,std::default_delete > > > * _Ln) thread_pool_test.exe!std::_LaunchPad,std::default_delete > > >::_Go() thread_pool_test.exe!std::_Pad::_Call_func(void * _Data) ucrtbased.dll!0fa27e48() [Frames below may be incorrect and/or missing, no symbols loaded for ucrtbased.dll] ucrtbased.dll!0fa27b8b() kernel32.dll!@BaseThreadInitThunk@12 () ntdll.dll!___RtlUserThreadStart@8 () ntdll.dll!__RtlUserThreadStart@8 ()
Linux 调用堆栈:
[New Thread 0x7ffff6f5d700 (LWP 4395)]
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff6f5d700 (LWP 4395)]
__GI___pthread_mutex_lock (mutex=0x0)
at ../nptl/pthread_mutex_lock.c:66
66 ../nptl/pthread_mutex_lock.c: No such file or directory.
(gdb) bt
#0 __GI___pthread_mutex_lock (mutex=0x0)
at ../nptl/pthread_mutex_lock.c:66
#1 0x0000000000401f53 in __gthread_mutex_lock (__mutex=0x50)
at /usr/include/x86_64-linux-gnu/c++/4.9/bits/gthr-default.h:748
#2 0x00000000004023ba in std::mutex::lock (this=0x50)
at /usr/include/c++/4.9/mutex:135
#3 0x000000000040370a in std::lock_guard<std::mutex>::lock_guard (
this=0x7ffff6f5cd10, __m=...) at /usr/include/c++/4.9/mutex:377
#4 0x00000000004030fa in work_stealing_queue::try_steal (this=0x0,
res=...) at Source.cpp:250
#5 0x00000000004032c8 in thread_pool_steal::pop_task_from_other_thread_queue (this=0x7fffffffdac0, task=...) at Source.cpp:302
#6 0x00000000004035e4 in thread_pool_steal::run_pending_task (
this=0x7fffffffdac0) at Source.cpp:358
#7 0x00000000004031ba in thread_pool_steal::worker_thread (
this=0x7fffffffdac0, my_index_=0) at Source.cpp:283
#8 0x000000000040d3d4 in std::_Mem_fn<void (thread_pool_steal::*)(unsigned int)>::operator()<int, void>(thread_pool_steal*, int&&) const (
this=0x62af78, __object=0x7fffffffdac0)
at /usr/include/c++/4.9/functional:569
#9 0x000000000040cec9 in std::_Bind_simple<std::_Mem_fn<void (thread_pool_steal::*)(unsigned int)> (thread_pool_steal*, int)>::_M_invoke<0ul, 1ul>(std::_Index_tuple<0ul, 1ul>) (this=0x62af68)
at /usr/include/c++/4.9/functional:1700
#10 0x000000000040c87f in std::_Bind_simple<std::_Mem_fn<void (thread_pool_steal::*)(unsigned int)> (thread_pool_steal*, int)>::operator()() (
this=0x62af68) at /usr/include/c++/4.9/functional:1688
#11 0x000000000040c4ea in std::thread::_Impl<std::_Bind_simple<std::_Mem_fn<void (thread_pool_steal::*)(unsigned int)> (thread_pool_steal*, int)> >::_M_run() (this=0x62af50) at /usr/include/c++/4.9/thread:115
#12 0x00007ffff78f7e40 in ?? ()
from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#13 0x00007ffff7bc4182 in start_thread (arg=0x7ffff6f5d700)
at pthread_create.c:312
#14 0x00007ffff735e47d in clone ()
at ../sysdeps/unix/sysv/linux/x86_64/clone.S:111
代码:
class work_stealing_queue
{
private:
typedef function_wrapper data_type;
std::deque<data_type> the_queue;
mutable std::mutex the_mutex;
bool empty() const
{
std::lock_guard<std::mutex> lock(the_mutex);
return the_queue.empty();
}
bool try_steal(data_type& res)
{
std::lock_guard<std::mutex> lock(the_mutex);
if (the_queue.empty())
{
return false;
}
res = std::move(the_queue.back());
the_queue.pop_back();
return true;
}
};
class thread_pool_steal
{
typedef function_wrapper task_type;
std::atomic_bool done;
threadsafe_queue<task_type> pool_work_queue;
std::vector<std::unique_ptr<work_stealing_queue> > queues;
std::vector<std::thread> threads;
static thread_local work_stealing_queue* local_work_queue;
static thread_local unsigned int my_index;
join_threads joiner;
bool pop_task_from_other_thread_queue(task_type& task)
{
for (unsigned i = 0; i<queues.size(); ++i)
{
unsigned const index = (my_index + i + 1) % queues.size();
if (queues[index]->try_steal(task))
{
return true;
}
}
return false;
}
public:
thread_pool_steal() : done(false), joiner(threads)
{
unsigned const thread_count = std::thread::hardware_concurrency();
try
{
for (auto i = 0; i<thread_count; ++i)
{
queues.push_back(std::unique_ptr<work_stealing_queue>(std::make_unique<work_stealing_queue>()));
threads.push_back(std::move(std::thread(&thread_pool_steal::worker_thread, this, i)));
}
}
catch (...)
{
done = true;
throw;
}
};
~thread_pool_steal()
{
done = true;
};
最佳答案
在 Linux 中,必须在调用 pthread_mutex_lock() 之前初始化“the_mutex”。请参阅 Unix 网络编程第 1 卷第 2 版。 (W. 理查德史蒂文斯)p.626。它说“如果一个互斥变量是静态分配的,我们必须将它初始化为常量 PTHREAD_MUTEX_INITIALIZER。”它看起来像这样:
pthread_mutex_t the_mutex = PTHREAD_MUTEX_INITIALIZER;
关于C++互斥锁定错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31015659/
我对 Java 并发性比较陌生(还没有阅读 JCIP,但它在我的列表中!)并且我有一个关于锁定行为的问题。具体来说,Java 是锁定对象的引用,还是锁定对象本身? 代码示例(不是 sscce,因为我不
我的团队使用 TortoiseSVN 编写版本控制代码。有时,有人使用“获取锁定”选项。是否有可能看到解决方案中的锁? 最佳答案 http://tortoisesvn.net/docs/nightly
我在使用 SVN 时遇到了一个小问题。 当我跑 svn stat我明白了: ~ some/dir 当我跑 svn commit -m "test"我明白了:svn: working copy
我启用了 jenkins 安全性,认为它会提示我创建一个帐户。我尝试在 c:/program files/jenkins 中删除和编辑我的 config.xml 文件,但我不确定如何在没有访问权限的情
实现与 S3 结合使用的简单锁定机制的推荐方法是什么? 我想做的例子: 通过对象 ID 获取锁 从 S3 读取对象 修改数据 将对象写入 S3 释放锁 理想情况下寻找基于云的锁定机制。我可以在本地使用
找到这个here : 一般来说,在以下任何情况下,请考虑在列上创建索引: 索引列上存在引用完整性约束,或者列。索引是避免全表锁的一种方法,否则,如果您更新父表主键,则需要,合并到父表中,或从父表中删除
在我的程序中,我将把每个“ block ”数据存储在一个单独的文件中。多个线程都会读取和写入各种文件,我想避免因未正确同步而可能出现的问题。本质上,我想要一个设置,其中每个文件的行为就好像它有自己的
我想使用此script作为资源,通过使用Windows API(重置管理器)与Go for Windows中的内容相同 到目前为止,我的代码是 Rstrtmgr := syscall.NewLazyD
这里的问题是:“这些选择中的哪一个对于线程安全选择的剧院具有最佳性能?” public static List lockList = initializeLocks(); public boolean
我有一个侧面菜单,单击图标时打开,单击页面或单击菜单上的项目时关闭。我正在尝试实现锁定,因此当单击锁定图标时,即使您单击菜单项或页面,菜单也不会关闭。 我能够将图标从锁定图标更改为解锁图标,但我在停止
使用 TRueType 字体编写 SDL 程序。我调用 TTF_Init() 来初始化 TTF 并使用 TTF_OpenFont( name, size ) 打开我的字体。 我有一个例程,可以使用以下
我正在尝试调试基于运行 FreeRTOS 的 STM32F3 uC 的应用程序。我已在应用程序的线程上下文中的随机位置手动将 PSP 设置为无效值(例如 0),希望触发 memManageFault/
我有以下 C# 代码: 1. List bandEdgeList; 2. 3. bandEdgeList = CicApplication.BandEdgeCache.Where(r
我正在用骰子制作游戏。这个想法是持有/锁定骰子。我把骰子做成按钮,这样现在就可以点击它们了。示例:我抛出一个“6”和一个“1”。我点击“6”,所以现在只会抛出“1”。 我对这个有点迷失了,我需要创建
我正在使用以下代码下载约 200mb 的播客并将其写入文档目录: var podcastRequest = NSURLRequest(URL: audioUrl) NSURLConnection.se
下面的类 DoStuff 启动一个线程并同步以保护监听器对象在 null 时不被访问。 现在,当从外部访问 DoStuff 类函数 setOnProgressListener() 时,我遇到了问题,因
我正在编写一个使用巨大背景 Canvas 的网站。我试图锁定浏览器调整大小处理程序以避免滚动问题(背景越界等) 这是我第一次做一个完整的后台网站。任何有关优化的建议(png 大小 580.72 KB
我是 C# 和线程的新手,我有这个问题要解决: 我有一个处理一些数据的线程,它会不时(必要时)触发我在启动线程之前设置的事件方法 (DataProcessor)。该线程位于专有 dll 中。所以我不能
我正在使用相机,我使用的是文档中给出的完全相同的示例: http://developer.android.com/resources/samples/ApiDemos/src/com/example/
我有几个座位可供用户预订。同一时间,只有一个用户可以参与预订过程,这样同一个座位就不会被多个用户预订。在我的 Java 代码中,我使用了“synchronized”关键字来完成它。这行得通。 但是,现
我是一名优秀的程序员,十分优秀!