c++ - 为什么下面的多线程代码会导致 SIGABRT ?

标签 c++ c++11 c++14

下面的代码在使用启用互斥保护的开关标志运行时会崩溃。即运行为./code.out 1

现在,核心转储的堆栈跟踪指向内存分配问题,这可能是由于线程的每次插入都会导致整个 vector 的重新分配,因为最初大小增加超过了保留的内存。

痕迹是:

\#0  0x00007f1582ce6765 in raise () from /lib64/libc.so.6
\#1  0x00007f1582ce836a in abort () from /lib64/libc.so.6
\#2  0x00007f1582d27710 in __libc_message () from /lib64/libc.so.6
\#3  0x00007f1582d2feaa in _int_free () from /lib64/libc.so.6
\#4  0x00007f1582d3340c in free () from /lib64/libc.so.6
\#5  0x0000000000403348 in __gnu_cxx::new_allocator<int>::deallocate(int*, unsigned long) ()
\#6  0x00000000004029b2 in std::allocator_traits<std::allocator<int> >::deallocate(std::allocator<int>&, int*, unsigned long) ()
\#7  0x00000000004020ae in std::_Vector_base<int, std::allocator<int> >::_M_deallocate(int*, unsigned long) ()
\#8  0x0000000000401e4e in void std::vector<int, std::allocator<int> >::_M_emplace_back_aux<int const&>(int const&) ()
\#9  0x0000000000401707 in std::vector<int, std::allocator<int> >::push_back(int const&) ()
\#10 0x0000000000401212 in pushItem(int const&) ()
\#11 0x0000000000403d88 in void std::_Bind_simple<void (*(unsigned int))(int const&)>::_M_invoke<0ul>(std::_Index_tuple<0ul>) ()
\#12 0x0000000000403cd3 in std::_Bind_simple<void (*(unsigned int))(int const&)>::operator()() ()
\#13 0x0000000000403c6e in std::thread::_State_impl<std::_Bind_simple<void (*(unsigned int))(int const&)> >::_M_run() ()
\#14 0x00007f158364f5cf in ?? () from /lib64/libstdc++.so.6
\#15 0x00007f15839235ca in start_thread () from /lib64/libpthread.so.0
\#16 0x00007f1582db50ed in clone () from /lib64/libc.so.6

当我运行与 reserve(20) 相同的程序时,它工作正常。问题是这里发生了什么。我也假设有互斥保护,如果发生这种情况,那么我就缺少了一些本质上有缺陷的东西 与 vector 接口(interface)本身。请指导!!

#include<iostream>
#include<vector>
#include<mutex>
#include<thread>
#include<algorithm>

using namespace std;

std::vector<int> g_vector;
std::mutex g_mutex;
bool demoFlag = false;

void pushItem(const int& ref)
{

    if(demoFlag)
    {
        cout << "Locking is enabled. so i am locking" << endl;
        std::lock_guard<std::mutex> lock(g_mutex);
    }

    g_vector.push_back(ref);
}


int main(int argc, char* argv[])
{

    if(argc == 2)
        demoFlag = atoi(argv[1]);

    g_vector.reserve(10);

    for(unsigned int i=0; i<10; ++i)
        g_vector.push_back(i);

    std::vector<std::thread> threads;
    for(unsigned int i=0; i<10; ++i)
        threads.push_back(std::thread(pushItem,i));

    std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join));

    for(const auto& ref : g_vector)
        cout << "Item is = " << ref << " , ";
    cout << endl;


}

最佳答案

您的锁未被使用。一些代码应该是这样的:

void pushItem(const int& ref)
{
    std::lock_guard<std::mutex> lock(g_mutex);
    cout << "I am locking" << endl;

    g_vector.push_back(ref);
}

关于c++ - 为什么下面的多线程代码会导致 SIGABRT ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44558017/

相关文章:

c++ - Schwarz 计数器和可变参数模板

c++ - 如何在扩展监视器上显示 QLabel

c++ - 为什么 C++0x 右值引用不是默认值?

c++ - 非静态成员函数的 decltype 格式不正确吗?

c++ - 在自定义迭代器上应用 reverse_iterator 后引用失效

c++ - 单例:是否存在内存泄漏?

c++ - 指向类函数数组的指针

c++ - Win7 和 Win10 之间的定时器差异

c++ - 为什么这个模板变量会导致编译器警告?

c++ - 从 Qt/C++-App 并行运行两个 Lua 函数