admin管理员组

文章数量:1122846

I have a multithreaded program in C++.

Here's a brief pseudo-code of the important bits and pieces:

main.cpp

main {
    try {
        boost::asio::io_context ioc;

        // 2. Start the D-Bus server, read and handle configuration.
        std::shared_ptr<classA> shared_classA = std::make_shared<classA>(ioc);

        // Continue to run, should not return unless error.
        ioc.run();
    }
    catch (const std::bad_alloc& e) {
      return (-1);
    }
    catch (const std::exception& e)
    {
        return (-1);
    }
    return 0;
   

}

classA.cpp

classA::classA(boost::asio::io_context& ioc) : _ioc(ioc) 
{
    try
    {
        std::thread d( [&]() -> void {
            
         /* Some DBUS related initialization for creating dbus objects 
            at a particular location on the dbus tree

            */
            // Create a new dbus instance
            auto b = sdbusplus::bus::new_default();         


            std::thread dh( [&]() -> void {
                try
                {
                       // Create DataHandler and a pointer to be accessed.
                       classB_sptr = std::make_shared<classB>();
                   
                       // Create data poller
                       Poller dp(classB_sptr);
                   
                       // Ready to start the poller thread (this is a separate thread)
                       /* this function creates a new thread with a polling function
                          
                          */
                       if (!dp.start())
                       {
                           // log exception to file
                       }

                       while (true)
                       {
                           std::this_thread::sleep_for(std::chrono::seconds(1)); // To prevent tight loop
                       }                
                }
                catch (const std::exception& e)
                {
                    // print exception to log file
                }
            });
            dh.detach(); // detach inner thread

            // Create DBUS PropertiesChanged handler for aforementioned object
            match = std::make_unique<sdbusplus::bus::match_t>(
                    /* args required for this match handler */
            )
            while (1) {
                b.process_discard();
                b.wait();
            }
        } );
        d.detach(); // detach outer thread
    }
    catch (const sdbusplus::exception::SdBusError& e)
    {
        // print exception to log file
    }
    catch (const std::bad_alloc& e)
    {
        // print exception to log file
    }
    catch (const std::exception& e)
    {
        // print exception to log file
    }
}

class B.cpp

classB::classB() {
    /*
       create a modbus connection and store a pointer to it in one of
       classB's members
     */
}

Poller.cpp

// ctor
Poller::Poller(std::shared_ptr<classB> classB_sptr): shared_classB(classB_sptr)) {

}

Poller::start () // start the poller thread
/*
   create a std::thread thread and run a loop in it polling over a modbus connection. This thread is
   also detached

*/

Main thread calls a constructor (ctor). Ctor then goes on to create a thread using std::thread using a lambda function. Inside the lambda, it spawns another thread and detaches it. This inner thread creates a data poller thread that runs a semi tight loop monitoring the system dbus.

At the end of the outer lambda function, we wait for messages on the system dbus.

Now coming on to my approach to debug this: I want to break at the point one of these threads exit. So I open them with gdb, put a breakpoint on pthread_exit and pthread_enter as well as catch throw to catch any exceptions.

I do hit the pthread_enter breakpoint however none of the other breakpoints are hit. I've tried single stepping and on cue at a given point, all the LWPs are exited but none of the other breakpoints are hit.

I'm wondering why that is the case and how do I go about finding what's causing these threads to die on themselves?

本文标签: multithreadingC thread exiting without a noticeneed help debugging with gdbStack Overflow