我被困住了,任何帮助都将不胜感激!我正在使用example中的多线程服务器uWebSockets。该代码创建了多个线程,这些线程在端口3030上侦听客户端。我使用以下代码编译了代码:"g++ -std=c++17 -o server_exe Test_Server.cpp -luWS -lssl -lz -lcrypto -lpthread"
该代码有时有效,但有时会给出分段错误。
代码如下。
Test_Server.cpp
#include "uWS.h"
#include <thread>
#include <algorithm>
#include <iostream>
int main() {
std::vector<std::thread *> threads(std::thread::hardware_concurrency());
std::transform(threads.begin(), threads.end(), threads.begin(), [](std::thread *t) {
return new std::thread([]() {
uWS::Hub h;
h.onMessage([](uWS::WebSocket<uWS::SERVER> *ws, char *message, size_t length, uWS::OpCode opCode) {
ws->send(message, length, opCode);
});
// This makes use of the SO_REUSEPORT of the Linux kernel
// Other solutions include listening to one port per thread
// with or without some kind of proxy inbetween
if (!h.listen(3030, nullptr, uS::ListenOptions::REUSE_PORT)) {
std::cout << "Failed to listen" << std::endl;
}
h.run();
});
});
std::for_each(threads.begin(), threads.end(), [](std::thread *t) {
t->join();
});
}
以下是核心转储的回溯记录:
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x000000395a673718 in ?? () from /usr/lib64/libcrypto.so.10 [Current thread is 1 (Thread 0x7fd51e4ca700 (LWP 25963))] Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 keyutils-libs-1.4-4.el6.x86_64 krb5-libs-1.10.3-10.el6_4.6.x86_64 libcom_err-1.41.12-18.el6.x86_64 libgcc-4.4.7-4.el6.x86_64 libselinux-2.0.94-5.3.el6_4.1.x86_64 libstdc++-4.4.7-4.el6.x86_64 openssl-1.0.1e-15.el6.x86_64 zlib-1.2.3-29.el6.x86_64 (gdb) bt full
#0 0x000000395a673718 in ?? () from /usr/lib64/libcrypto.so.10 No symbol table info available.
#1 0xca62c1d6ca62c1d6 in ?? () No symbol table info available.
#2 0xca62c1d6ca62c1d6 in ?? () No symbol table info available.
#3 0xca62c1d6ca62c1d6 in ?? () No symbol table info available.
#4 0xca62c1d6ca62c1d6 in ?? () No symbol table info available.
#5 0xca62c1d6ca62c1d6 in ?? () No symbol table info available.
#6 0xca62c1d6ca62c1d6 in ?? () No symbol table info available.
#7 0xca62c1d6ca62c1d6 in ?? () No symbol table info available.
#8 0xca62c1d6ca62c1d6 in ?? () No symbol table info available.
#9 0x000000395a9db7f4 in ?? () from /usr/lib64/libcrypto.so.10 No symbol table info available.
#10 0x00007fd5080048b0 in ?? () No symbol table info available.
#11 0x000000000000000b in ?? () No symbol table info available.
#12 0x000000395a670b97 in SHA1_Update () from /usr/lib64/libcrypto.so.10 No symbol table info available.
#13 0x000000395a6e3316 in ?? () from /usr/lib64/libcrypto.so.10 No symbol table info available.
#14 0x000000395a6e36d8 in ?? () from /usr/lib64/libcrypto.so.10 No symbol table info available.
#15 0x000000395ca42e23 in SSL_CTX_new () from /usr/lib64/libssl.so.10 No symbol table info available.
#16 0x00007fd51f8fc43d in uS::Node::Node(int, int, int, bool) () from /usr/local/lib/libuWS.so No symbol table info available.
#17 0x0000000000402de0 in uWS::Hub::Hub(int, bool, unsigned int) () No symbol table info available.
#18 0x0000000000401cad in main::{lambda(std::thread*)#1}::operator()(std::thread*) const::{lambda()#1}::operator()() const () No symbol table info available.
#19 0x0000000000402403 in void std::__invoke_impl<void, main::{lambda(std::thread*)#1}::operator()(std::thread*) const::{lambda()#1}>(std::__invoke_other, main::{lambda(std::thread*)#1}::operator()(std::thread*) const::{lambda()#1}&&) () No symbol table info available.
#20 0x0000000000401fd3 in std::__invoke_result<main::{lambda(std::thread*)#1}::operator()(std::thread*) const::{lambda()#1}>::type std::__invoke<main::{lambda(std::thread*)#1}::operator()(std::thread*) const::{lambda()#1}>(main::{lambda(std::thread*)#1}::operator()(std::thread*) const::{lambda()#1}&&, (std::__invoke_result&&)...) () No symbol table info available.
#21 0x0000000000402778 in decltype (__invoke((_S_declval<0ul>)())) std::thread::_Invoker<std::tuple<main::{lambda(std::thread*)#1}::operator()(std::thread*) const::{lambda()#1}> >::_M_invoke<0ul>(std::_Index_tuple<0ul>) () No symbol table info available.
#22 0x0000000000402749 in std::thread::_Invoker<std::tuple<main::{lambda(std::thread*)#1}::operator()(std::thread*) const::{lambda()#1}> >::operator()() () No symbol table info available.
#23 0x0000000000402728 in std::thread::_State_impl<std::thread::_Invoker<std::tuple<main::{lambda(std::thread*)#1}::operator()(std::thread*) const::{lambda()#1}> > >::_M_run() () No symbol table info available.
#24 0x000000000040414f in execute_native_thread_routine () No symbol table info available.
#25 0x00000039542079d1 in start_thread () from /lib64/libpthread.so.0 No symbol table info available.
#26 0x0000003953ae8b6d in clone () from /lib64/libc.so.6 No symbol table info available.
操作系统:RHEL 6