Hier ist ein kurzer Pseudocode der wichtigen Teile:
Code: Select all
main.cpp
main {
try {
boost::asio::io_context ioc;
// 2. Start the D-Bus server, read and handle configuration.
std::shared_ptr shared_classA = std::make_shared(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();
// 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(
/* 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_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
*/
Am Ende der äußeren Lambda-Funktion warten wir auf Nachrichten auf dem System-Dbus.
Jetzt komme ich zu meinem Ansatz, dies zu debuggen:
Ich möchte an dem Punkt abbrechen, an dem einer dieser Threads beendet wird. Also öffne ich sie mit gdb, setze einen Haltepunkt auf pthread_exit und pthread_enter sowie Catch Throw, um etwaige Ausnahmen abzufangen.
Ich treffe der pthread_enter-Haltepunkt, jedoch wird keiner der anderen Haltepunkte erreicht. Ich habe es mit Einzelschritten versucht und an einem bestimmten Punkt werden alle LWPs beendet, aber keiner der anderen Haltepunkte wird erreicht.
Ich frage mich, warum das so ist und wie Muss ich herausfinden, was dazu führt, dass diese Threads von selbst absterben?