【问题标题】:C++ mutex locking errorC++ 互斥锁错误
【发布时间】:2015-09-10 00:17:49
【问题描述】:

我正在使用具有工作窃取功能的线程池,但是每当程序尝试将互斥锁锁定在工作队列中时,我都会收到异常错误。

我已经在 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 * 目标) 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>) 第 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(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() [以下帧可能不正确和/或丢失,没有为 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;
};

【问题讨论】:

  • 可能与静态互斥对象的线程安全初始化有关。我现在正在努力解决类似的问题。您是否使用 -pthread 进行编译/链接?
  • @iggy 是的,当我在 linux 计算机上编译时,我使用 pthreads。感谢您的提示,我将研究错误是否与静态初始化有关。
  • 其实是work_stealing_queue::try_steal (this=0x0,所以你有一个空对象
  • -fsanitize=address 编译它,看看你得到了什么
  • 用下面的g++ -std="c++14" -g -fsanitize=address Source.cpp -o test -lpthread 编译得到

标签: c++ multithreading concurrency mutex c++14


【解决方案1】:

在 Linux 中,必须在调用 pthread_mutex_lock() 之前初始化“the_mutex”。 请参阅 Unix 网络编程第 1 卷第 2 版。 (W. Richard Stevens)第 626 页。 它说“如果一个互斥变量是静态分配的,我们必须将它初始化为常量 PTHREAD_MUTEX_INITIALIZER。”它看起来像这样:

pthread_mutex_t the_mutex = PTHREAD_MUTEX_INITIALIZER;

【讨论】:

    【解决方案2】:

    出现此错误的原因似乎是 std::vector 不是线程安全的。 当主线程添加到 work_stealing_queue,queues 的向量时,新生成的线程调用 queues.size()。我的猜测是在队列中最新的 work_stealing_queue 准备好之前, size() 函数正在递增。解决方案是创建两个单独的循环,一个创建队列中的所有 work_stealing_queue,另一个使用函数 worker_thread 运行线程。

    for(auto i=0;i<thread_count;++i)
                {
                   queues.push_back(std::unique_ptr<work_stealing_queue>  (std::make_unique<work_stealing_queue>()));
                }
    
    for(auto i=0;i<thread_count;++i) // Seperate becuse std::vector is not thread safe and causes issues when size() is used
                {
                    threads.push_back(std::thread(&thread_pool_steal::worker_thread,this,i));
                }
    

    【讨论】:

    • 是的,这似乎有效!在我在这里找到你的答案之前,我一直在努力解决我认为与 Anthony Williams 书中相同的代码。
    猜你喜欢
    • 1970-01-01
    • 2015-10-06
    • 2022-07-31
    • 1970-01-01
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    相关资源
    最近更新 更多