调用线程终止而没有活动异常

时间:2011-06-06 20:01:59

标签: c++ multithreading network-programming pthreads sfml

我一直在做游戏的线程网络,但服务器随机死亡,而我一直在测试网络,以便我有几个客户端连接并发送一堆数据包并断开然后再连接回来。

我正在使用带有SFML / Network和SFML / System线程的c ++。我有一个线程,一旦建立连接就会监听服务器中的连接,它会创建两个用于发送和接收数据包的新线程。事件处理程序和发送/接收线程与两个std :: queues共享数据。我一直在尝试使用gdb调试崩溃,但我不是那么经验,所以我正在寻求帮助。

发生崩溃时,这是gdb控制台输入。

OUT: 10 1 HELLO
IN: 10 0 LOLOLOL
OUT: 10 1 HELLO
IN: 10 0 LOLOLOL
OUT: 10 1 HELLO
Out thread killed by in thread!
In thread died!
New client connected!
[Thread 0x34992b70 (LWP 16167) exited]
[New Thread 0x3118bb70 (LWP 16186)]
terminate called without an active exception

Program received signal SIGABRT, Aborted.
[Switching to Thread 0x35193b70 (LWP 16166)]
0x00110416 in __kernel_vsyscall ()

这是回溯:

(gdb) backtrace
#0  0x00110416 in __kernel_vsyscall ()
#1  0x46a0967f in raise (sig=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
#2  0x46a0afb5 in abort () at abort.c:92
#3  0x47b8af0d in __gnu_cxx::__verbose_terminate_handler () at ../../../../libstdc++-v3/libsupc++/vterminate.cc:95
#4  0x47b88c84 in __cxxabiv1::__terminate (handler=0x47b8adc0 <__gnu_cxx::__verbose_terminate_handler()>) at ../../../../libstdc++-v3/libsupc++/eh_terminate.cc:40
#5  0x47b88cc0 in std::terminate () at ../../../../libstdc++-v3/libsupc++/eh_terminate.cc:50
#6  0x47b8878f in __cxxabiv1::__gxx_personality_v0 (version=1, actions=10, exception_class=890844228, ue_header=0x35193dc0, context=0x35192ea0)
    at ../../../../libstdc++-v3/libsupc++/eh_personality.cc:669
#7  0x46bdbfbe in _Unwind_ForcedUnwind_Phase2 (exc=0x35193dc0, context=0x35192ea0) at ../../../gcc/unwind.inc:175
#8  0x46bdc3a9 in _Unwind_ForcedUnwind (exc=0x35193dc0, stop=0x46b76fc0 <unwind_stop>, stop_argument=0x35193444) at ../../../gcc/unwind.inc:207
#9  0x46b794e2 in _Unwind_ForcedUnwind (exc=0x35193dc0, stop=0x46b76fc0 <unwind_stop>, stop_argument=0x35193444) at ../nptl/sysdeps/pthread/unwind-forcedunwind.c:132
#10 0x46b77141 in __pthread_unwind (buf=<optimized out>) at unwind.c:130
#11 0x46b6f5bb in __do_cancel () at ../nptl/pthreadP.h:265
#12 sigcancel_handler (sig=<optimized out>, si=<optimized out>, ctx=<optimized out>) at nptl-init.c:202
#13 sigcancel_handler (sig=32, si=0x35192f7c, ctx=0x35192ffc) at nptl-init.c:155
#14 <signal handler called>
#15 0x08049930 in out (data=0xb761c798) at src/layer7.cpp:40
#16 0x0804b8d7 in sf::priv::ThreadFunctorWithArg<void (*)(networkdata*), networkdata*>::Run (this=0xb761c7c8) at /usr/local/include/SFML/System/Thread.inl:48
#17 0x00116442 in sf::Thread::Run() () from /home/toni/ProjectRepos/sfml/build/lib/libsfml-system.so.2
#18 0x001166df in sf::priv::ThreadImpl::EntryPoint(void*) () from /home/toni/ProjectRepos/sfml/build/lib/libsfml-system.so.2
#19 0x46b70c5e in start_thread (arg=0x35193b70) at pthread_create.c:305
#20 0x46ab4b4e in clone () at ../sysdeps/unix/sysv/linux/i386/clone.S:133

这是src / layer7.cpp

中的线程代码
void out(networkdata * data) {
    bool running = true;
    while(running) {
        if(data->pipe_out->pipe_empty() == false) {
            sf::Packet packet = data->pipe_out->pop_message();
            if(data->socket->Send(packet) == sf::Socket::Disconnected) {
                data->thread_in->Terminate();
                std::cout << "In thread killed by out thread!" << std::endl;
                running = false;
            }
        }
    }
    std::cout << "Out thread died!" << std::endl;
}
  • 第40行是while(运行)之后的第一个if关键字。
  • data-&gt; pipe_out-&gt; pipe_empty()调用queue-&gt; empty()
  • 数据 - &gt; pipe_out-&gt; pop_message()是从队列中弹出前面的调用。
  • 然后它发送数据包并检查连接是否未断开
  • 如果socket断开连接,它将终止“in”线程并停止自己的线程。

2 个答案:

答案 0 :(得分:3)

您需要在data周围进行锁定,以防止从多个线程并发访问相同的数据结构。

答案 1 :(得分:0)

一个可能的原因是异常:应该用线程捕获异常。此外,看起来像data-&gt; thread_in-&gt; Terminate()发送取消请求,确保在这种情况下所有已建立的取消处理程序都正常工作。