C ++ Boost :: thread - 内核:陷阱一般保护

时间:2018-06-17 22:14:13

标签: c++ boost-thread coredump

我的代码使用了旧版本的boost 1.49,但是很多年前。现在我使用boost 1.67

编辑:我的项目包含使用相同二进制文件的服务器/客户端功能。 一个服务器启动,我可以发送收到的命令来启动自定义进程。这是下面的代码。

我确定了导致内核陷阱的行:

boost::thread th(Temporal::Acquire, transmit, ECONF);
  • 线程正在启​​动并调用参数中的函数,但启动的线程立即崩溃。 我不理解"一般保护"。

  • 我试图从try catch(std :: exception& e)中找​​到更多答案 但它似乎需要另一个捕手......没有什么可输出。

  • 试图理解libs / thread / src / pthread / thread.cpp中tls_destructor的处理,但是因为我已经通过将所有std替换为boost而没有解决问题来测试我的代码...

  • Valgrind完全没有错误。

有一种方法可以理解直接终止(不调用join或interrupt)?

使用标准线程启动服务器套接字(来自另一个文件)的部分,但我不认为这是问题的根源: 自从我开始我的项目以来,我从来没有混合std / boost的冲突。

coex.push_back(std::thread(Temporal::Listener, ECONF));

服务器部分:

#include "lobe.hpp"

#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

void    Temporal::Acquire(std::string transmit, Json::Value ECONF)
{
    syslog(LOG_NOTICE, "aquired");
    // Temporal::Transcode p(transmit, ECONF);
}

void    Temporal::Listener(Json::Value ECONF)
{
    socklen_t           t;
    std::string         transmit(100, 0);
    int                 PIPE_local, PIPE_remote, len;
    struct sockaddr_un  local, remote;

    int reuseaddr = 1;
    memset(&local, 0, sizeof(local));

    if((PIPE_local = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
        perror("socket");

    if(setsockopt(PIPE_local, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr)) == -1)
        perror(strerror(errno));

    local.sun_family = AF_UNIX;
    strncpy(local.sun_path, P_SOCK, sizeof(local.sun_path)-1);
    unlink(P_SOCK);
    len = strlen(local.sun_path) + sizeof(local.sun_family);


    if(bind(PIPE_local, (struct sockaddr *)&local, len) == -1)
        perror("bind");

    if(listen(PIPE_local, 5) == -1)
        perror("listen");

    for(;;)
    {
        syslog(LOG_INFO, "inside SOCK");
        int done, com_Listen, com_Talk;
        t = sizeof(remote);
        if((PIPE_remote = accept(PIPE_local, (struct sockaddr *)&remote, &t)) == -1)
            perror("accept");

        done = 0;
        do
        {
            com_Listen = read(PIPE_remote, &transmit[0], 99);
            if(com_Listen <= 0)
            {
                syslog(LOG_NOTICE, "<<-== %s", transmit.c_str());
                if(com_Listen < 0) perror("recv");
                done = 1;

                syslog(LOG_NOTICE, "received");
                boost::thread th(Temporal::Acquire, transmit, ECONF);
            }
        }while(!done);

        close(PIPE_remote);
        break;
    }
    close(PIPE_local);
    unlink(P_SOCK);

    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    //boost::this_thread::sleep_for(boost::chrono::seconds(1));
    Temporal::Listener(ECONF);
}

客户端部分:

systemd coredump的输出:

Jun 17 22:37:25 bytewild kernel: traps: EIE[8033] general protection ip:44f59c sp:7fd32bffecb0 error:0 in EIE[400000+233000]
Jun 17 22:37:25 bytewild EIE[7699]: aquired
Jun 17 22:37:25 bytewild systemd[1]: Started Process Core Dump (PID 8034/UID 0).
-- Subject: Unit systemd-coredump@49-8034-0.service has finished start-up
-- Defined-By: systemd
-- Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel

-- Unit systemd-coredump@49-8034-0.service has finished starting up.
-- 
-- The start-up result is RESULT.
Jun 17 22:37:25 bytewild systemd-coredump[8041]: Failed to get ACL: Operation not supported
Jun 17 22:37:26 bytewild systemd-coredump[8041]: Process 7699 (EIE) of user 1000 dumped core.

     Stack trace of thread 8033:
     #0  0x000000000044f59c tls_destructor (/data/dev/in/native/projects/eie/build/bin/EIE)
     #1  0x000000000045092a thread_proxy (/data/dev/in/native/projects/eie/build/bin/EIE)
     #2  0x0000000000500155 start_thread (/data/dev/in/native/projects/eie/build/bin/EIE)
     #3  0x00000000005707ff __clone (/data/dev/in/native/projects/eie/build/bin/EIE)

     Stack trace of thread 7700:
     #0  0x0000000000503623 __pthread_cond_timedwait (/data/dev/in/native/projects/eie/build/bin/EIE)
     #1  0x000000000041214b _ZN5boost18condition_variable13do_wait_untilERNS_11unique_lockINS_5mutexEEERKNS_6detail23mono_platform_timepointE (/data/dev/in/native/projects/eie/build/bin/EIE)
     #2  0x000000000040ebe4 _ZN8Temporal8ListenerEN4Json5ValueE (/data/dev/in/native/projects/eie/build/bin/EIE)
     #3  0x000000000041f58e _ZSt13__invoke_implIvPFvN4Json5ValueEEJS1_EET_St14__invoke_otherOT0_DpOT1_ (/data/dev/in/native/projects/eie/build/bin/EIE)
     #4  0x00000000004eb0ef execute_native_thread_routine (/data/dev/in/native/projects/eie/build/bin/EIE)
     #5  0x0000000000500155 start_thread (/data/dev/in/native/projects/eie/build/bin/EIE)
     #6  0x00000000005707ff __clone (/data/dev/in/native/projects/eie/build/bin/EIE)

     Stack trace of thread 7699:
     #0  0x0000000000504a21 __nanosleep (/data/dev/in/native/projects/eie/build/bin/EIE)
     #1  0x000000000056bfea __sleep (/data/dev/in/native/projects/eie/build/bin/EIE)
     #2  0x0000000000406e91 main (/data/dev/in/native/projects/eie/build/bin/EIE)
     #3  0x0000000000506dfa __libc_start_main (/data/dev/in/native/projects/eie/build/bin/EIE)
     #4  0x000000000040790a _start (/data/dev/in/native/projects/eie/build/bin/EIE)

以下是我的系统的示意图预览,以了解问题:

很抱歉,如果这很明显,但我很难过。有线索吗?

1 个答案:

答案 0 :(得分:1)

查看您的代码我认为问题出在这段代码中。

do
        {
            com_Listen = read(PIPE_remote, &transmit[0], 99);
            if(com_Listen <= 0)
            {
                syslog(LOG_NOTICE, "<<-== %s", transmit.c_str());
                if(com_Listen < 0) perror("recv");
                done = 1;

                syslog(LOG_NOTICE, "received");
                boost::thread th(Temporal::Acquire, transmit, ECONF);
            }
        }while(!done);

具体来说,就是您创建boost::thread的方式。这是一个基于堆栈的变量。一旦启动线程,其对象就会被销毁,并且DTor被调用。

我对thread的boost实现没有多少经验,但使用了std::thread,这主要是基于boost实现的模型。查看他们的班级documentation,有一个效果部分列出了销毁正在运行的线程的影响。

  

效果:    - 如果已定义BOOST_THREAD_DONT_PROVIDE_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE:   如果线程是可连接的,则调用detach(),DEPRECATED    - 如果已定义BOOST_THREAD_PROVIDES_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE:   如果线程是可连接的调用std :: terminate。摧毁*这个。

默认情况下,默认情况下调用terminate()就像std::thread对被销毁的可连接线程一样。看起来旧的行为是自动分离那些。

修改 阅读析构函数文档,了解当前与1.49的对比情况。

1.49 功效: 如果*这有一个相关的执行线程,则调用detach()。摧毁*这个。

现在,请阅读相同的当前代码(如上所列)。它声明它现在默认为terminate

<强> EDIT2

我建议每次从套接字接收输入时都不再启动新线程。相反,我建议创建一个具有恒定数量的后台线程的工作队列。每次收到新事件时,只需将工作对象添加到队列中,其中一个后台线程将处理响应。

#include <chrono>
#include <condition_variable>
#include <deque>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>

// Synchrnoized Output
std::mutex stdm;
template <typename T>
void Log(T const& t) {
    std::lock_guard<std::mutex> lk(stdm);
    std::cout << t << std::endl;
}

// Task Queue
// Has user provided list of task, which are handled on background threads
template <class T, int N = 4>
class TaskQueue {
    std::deque<T> work;                 // Holds work
    std::vector<std::thread> threads;   // Holds worker threads
    std::mutex m;                       // Holds lock for work container/running
    std::condition_variable cv;         // Worker threads wait on this
    bool running;                       // Inform the task queue if its running

    public:
    // Constructor, spins up worker threads and waits for work
    TaskQueue() : running{true} {
        threads.reserve(N);
        for (int i = 0; i < N; ++i) {
            // Build worker threads
            threads.emplace_back([&]() {
                // Normal running before queue destruction
                while (running) {
                    {
                        std::unique_lock<std::mutex> lk(
                            m);
                        cv.wait(lk, [&] {
                            return !running ||
                                   work.size() != 0;
                        });

                        // Extract work && Update work
                        // Queue
                        T t = std::move(work.front());
                        work.pop_front();

                        // Release lock before
                        // performing work
                        lk.unlock();

                        // Peform work
                        t();
                    }
                }

                // Empty Queue on destrucrtion
                bool hasMoreWork = true;
                do {
                    std::unique_lock<std::mutex> lk(m);
                    if ((hasMoreWork = work.size() > 0)) {
                        // Manage Work
                        T t = std::move(work.front());
                        work.pop_front();

                        // has more?
                        hasMoreWork = work.size() > 0;

                        // release lock
                        lk.unlock();

                        // perform work
                        t();
                    }
                } while (hasMoreWork);
            });
        }
    }

    ~TaskQueue() {
        // Inform queue its closing
        {
            std::lock_guard<std::mutex> lk(m);
            running = false;
        }

        // Inform all threads of change
        cv.notify_all();

        // Clear out remainings objects
        int workObjects = 0;
        bool queueCleared = false;
        do {
            {
                std::lock_guard<std::mutex> lg(m);
                queueCleared = (workObjects = work.size()) == 0;
                Log("Queue Has Remaining: " +
                    std::to_string(workObjects));
            }

            // Give worker threads time to work
            std::this_thread::sleep_for(
                std::chrono::milliseconds(250));
        } while (!queueCleared);

        // If any threads are still processing, join them to the current
        // thread or else terminate() is called
        for (int i = 0; i < N; i++) {
            if (threads[i].joinable()) threads[i].join();
        }
    }

    template <class... Args>
    void emplace_back(Args&&... args) {
        {
            std::lock_guard<std::mutex> lk(m);
            if (running)
                work.emplace_back(std::forward<Args>(args)...);
        }
        cv.notify_one();
    }
};

// The actual task that performs work
struct Task {
    std::string transmit;
    std::string ECONF;

    Task() : transmit(""), ECONF("") {}
    Task(std::string&& t, std::string&& e) : transmit(t), ECONF(e) {}

    void operator()() {
        std::thread::id tid = std::this_thread::get_id();
        std::hash<std::thread::id> hasher;
        Log(transmit + ':' + ECONF +
            " Process Time: 500ms One Thread: " +
            std::to_string(hasher(tid)));

        // Fake work to consume thread
        std::this_thread::sleep_for(std::chrono::milliseconds(500));
    }
};

int main() {
    TaskQueue<Task> tp;
    for (int i = 0; i < 200; ++i) {
        // Add work
        tp.emplace_back("Transmit: " + std::to_string(i),
                "ECONF: " + std::to_string(i));

        // Simulate waiting for the next event from socket
        std::this_thread::sleep_for(std::chrono::milliseconds(25));
    }
    return 0;
}

可以进行大量的改进,但这应该给你一个粗略的概述。希望这会有所帮助。