2017-06-15 71 views
0

下面的代碼在使用開關標誌啓用互斥保護的情況下運行時會爆炸。即以./code.out運行1爲什麼下面的多線程代碼導致SIGABRT?

現在,核心轉儲的堆棧跟蹤指向內存分配問題,可能是因爲線程中的每個插入都會導致整個向量的重新分配,這是由於大小超過最初保留內存。

的軌跡是:

\#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 

當我運行同一程序與儲備(20),它工作正常。問題在於這裏發生了什麼。我假設互斥體保護也是如果發生這種情況,那麼我缺少的東西本質上是有缺陷的 與矢量接口本身。請指導!

#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; 


} 
+1

你認爲你使用該互斥鎖定了什麼? – Praetorian

+0

您正在進行數據競賽,這是未定義的行爲 –

+0

明白了吧! :(我可憐的if循環esentially在結束後解鎖:((對我愚蠢 –

回答

1

您的鎖不被使用。一些代碼應該是這樣的:

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

    g_vector.push_back(ref); 
} 
相關問題