Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, am getting SEGEXIT signal from one thread, please help me what is the issue ? and then how to see this ?...

C++
string l_Resp = "Code"+"For Project";
log("String formation done successfully, ready for pushing");
pthread_mutex_lock(&g_MsgListMutex);
g_DeliveryRpt.push_back(l_Resp);
pthread_mutex_unlock(&g_MsgListMutex);
log("Msg Push Successful");
g_DeliverRpt is list<string>

pstack log: called from signal handler with signal 0 (SIGEXIT)
00000001003766dc _ZNSt20_List_const_iteratorISsEppEv (ffffffff75bc9eb0, ffffffff75bc9eb8, 0, 0, 0, 0) + 14
000000010036d8ac _ZSt10__distanceISt20_List_const_iteratorISsEENSt15iterator_traitsIT_E15difference_typeES3_S3_St18input_iterator_tag (10bb35c20, 100604d38, 0, 3e, 101dbff39, 0) + 54
0000000100365adc _ZSt8distanceISt20_List_const_iteratorISsEENSt15iterator_traitsIT_E15difference_typeES3_S3_ (10bb35c20, 100604d38, ffffffff75bc9ef0, ffffffff7ee008c8, ff0000, 80808080) + 50
0000000100360a64 _ZNKSt4listISsSaISsEE4sizeEv (100604d38, ffffffff75bca3c0, 100437e38, 10c6e2370, c75, ffffffff75bca430) + 40
00000001000a5438 _ZN10Client15DecodeDeliverSmEv (ffffffff75bcbaa0, 10358b9f0, 1, 0, 4b0427000102001e, 0) + 7190
00000001000ae588 _ZN10Client14ProcessMessageEv (ffffffff75bcbaa0, ffffffff7e509200, 0, 3e8, 0, f4240) + 21b4
00000001000ba518 _Z12ThClientPv (0, 1fc000, 0, 0, 1000ba318, 1) + 200


What I have tried:

i have analysis the log and code. then i find out the "log("Msg Push Successful");" line not printed in log file. After "log("String formation done successfully, ready for pushing");" this line that thread alone gets hung.
Posted
Updated 16-Sep-22 6:26am
v2
Comments
CPallini 16-Sep-22 7:50am    
"this line that thread alone gets hung"
This happens, if the thread cannot acquire the mutex.

Your code is incomplete. We don't know what the types of some of your data is like g_DeliveryRpt.

I would add another call to log after the mutex is acquired like this :
C++
string l_Resp( "Code For Project" );
log("String formation done successfully, ready for pushing");
pthread_mutex_lock( &g_MsgListMutex );
log( "mutex acquired - pushing '%s'", l_Resp.c_str() );
g_DeliveryRpt.push_back( l_Resp );
pthread_mutex_unlock( &g_MsgListMutex );
log( "Msg Push Successful - mutex released" );
Here's another little tip. I like to use what Paul DiLascia called excursion classes. They do something on construction and then undo it on destruction. This makes the process simple and automatic. Acquiring this mutex is a good candidate for this. Here is what a mutex excursion class might look like :
C++
class LockMutex
{
public:
    LockMutex( std::pthread_mutex_t * pmutex )  // constructor - acquires lock
    {
        m_pmtx = pmutex;
        std::pthread_mutex_lock( m_pmtx );
    }

    ~LockMutex()   // destructor - releases the lock
    {
        std::pthread_mutex_unlock( m_pmtx );
    }

public:
    std::pthread_mutex_t *  m_pmtx;
};
I used std:: there because this is something that can live in a header and I never, ever put using statements in header files because they can not be undone and that can cause problems.

Here is how you can use this class :
C++
string l_Resp( "Code For Project" );
log("String formation done successfully, ready for pushing");
{   // scoping for the lock
    LockMutex lock( &g_MsgListMutex );
    log( "mutex acquired - pushing '%s'", l_Resp.c_str() );
    g_DeliveryRpt.push_back( l_Resp );
}   // end of lock scope - now unlocked
log( "Msg Push Successful - mutex released" );
You don't have to remember to unlock the mutex because it automatic. You just have to pay attention to the scope of the object because the unlock happens when it falls out of scope and is destroyed.

This is important because if your thread is hanging that means something else has acquired the lock and has not released it yet. If you use this lock class everywhere then unlocking will always be automatic.
 
Share this answer
 
Comments
Richard MacCutchan 17-Sep-22 4:58am    
+5; that's worth writing up as a tip.
Rick York 17-Sep-22 12:24pm    
Thanks, good idea.
Rick York 17-Sep-22 17:37pm    
After looking around a little, I found there is std::lock_guard that does exactly this. I learned something new today.
Richard MacCutchan 18-Sep-22 3:11am    
Thanks, now I did too.
I can't tell what's happening based on the information that you've provided. The stack trace appears to reach some const_iterator function in std::list. Did it throw an exception?

The stack trace seems to be from another thread, because neither pthread_mutex_lock nor list::push_back appears in the traceback. I don't think the code fragment that you posted is the cause of the problem.

I just found an online demangler[^] so that we can get a better idea of what functions are in your traceback. Unfortunately, it still doesn't help me explain why your program is crashing:
C++
00000001003766dc std::_List_const_iterator<std::string>::operator++() (ffffffff75bc9eb0, ffffffff75bc9eb8, 0, 0, 0, 0)   14
000000010036d8ac std::iterator_traits<std::_List_const_iterator<std::string> >::difference_type std::__distance<std::_List_const_iterator<std::string> >(std::_List_const_iterator<std::string>, std::_List_const_iterator<std::string>, std::input_iterator_tag) (10bb35c20, 100604d38, 0, 3e, 101dbff39, 0)   54
0000000100365adc std::iterator_traits<std::_List_const_iterator<std::string> >::difference_type std::distance<std::_List_const_iterator<std::string> >(std::_List_const_iterator<std::string>, std::_List_const_iterator<std::string>) (10bb35c20, 100604d38, ffffffff75bc9ef0, ffffffff7ee008c8, ff0000, 80808080)   50
0000000100360a64 std::list<std::string, std::allocator<std::string> >::size() const (100604d38, ffffffff75bca3c0, 100437e38, 10c6e2370, c75, ffffffff75bca430)   40
00000001000a5438 _ZN10Client15DecodeDeliverSmEv (ffffffff75bcbaa0, 10358b9f0, 1, 0, 4b0427000102001e, 0)   7190
00000001000ae588 _ZN10Client14ProcessMessageEv (ffffffff75bcbaa0, ffffffff7e509200, 0, 3e8, 0, f4240)   21b4
00000001000ba518 _Z12ThClientPv (0, 1fc000, 0, 0, 1000ba318, 1)   200
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900