对我的程序在返回时挂起并等待 futex_wait_queue_me 通道感到沮丧

发布于 2024-11-25 00:49:37 字数 1977 浏览 1 评论 0原文

我正在开发一个基于服务器的项目。基本上,当服务器启动时,它需要绑定一个套接字端口(比如1935)。

我的问题是,如果我的程序的另一个实例正在运行,即正在使用套接字端口 1935,那么我的程序的新实例将挂在我的 main() 返回上。

错误信息:

Error starting server: SocketException: in Socket::bind: ADDRINUSE

当错误发生时,我的程序逻辑将尝试返回-1。在程序退出之前,return -1会首先释放系统资源,导致挂起的具体行是:

pthread_join ( tid_, 0 )

并且来自系统监视器(gnome-system-monitor),可以看到:

Process Name  | Status   | % CPU | Nice | ID    | Memory    | Waiting Channel
MyProgramName | Sleeping |   0   |  0   | 28573 | 300.0 KiB | futex_wait_queue_me

我的项目的多线程部分是别人写的,没有文档。所以我有点被困在这里了。

请帮帮我。

如果您需要更多信息,请告诉我。我会尽力补充。

这是我的 main() 逻辑:

MyServer server(host, port); // When `return -1` is called, this line will eventually call `pthread_join (tid, 0)`
try
{
    server.init(config);
}
catch (const MyException& e)
{
    DebugUtils::Error(boost::format("Error initializing server: %1% [%2%].")
                      % e.what() % e.GeneralMessage);
    return -1;
}
catch (const std::exception& e)
{
    DebugUtils::Error(boost::format("Error initializing server: %1%.")
                      % e.what());
    return -1;
}
catch (...)
{
    DebugUtils::Error("Unknown error initializing server.");
    return -1;
}

try
{
    server.start();
}
catch (const MyException& e)
{
    DebugUtils::Error(boost::format("Error starting server: %1% [%2%].")
                      % e.what() % e.GeneralMessage);
    return -1;
}
catch (const std::exception& e)
{
    DebugUtils::Error(boost::format("Error starting server: %1%.")
                      % e.what()); // This line output the error info
    return -1; // This line will first free the resource, so it will call the destructor of MyServer on the first line in this code snippet.
}
catch (...)
{
    DebugUtils::Error("Unknown error starting server.");
    return -1;
}

Peter

I'm working on a server-based project. Basically, when the server starts, it needs to bind a socket port (say 1935).

My problem is, if another instance of my program is running, that is, the socket port 1935 is being used, then a new instance of my program will hang on my main() return.

The Error info:

Error starting server: SocketException: in Socket::bind: ADDRINUSE

When error occurs, my program logic will try to return -1. Before the program exits, the return -1 will first free system resource, the specific line that causes the hanging is:

pthread_join ( tid_, 0 )

And from System Monitor (gnome-system-monitor), I can see:

Process Name  | Status   | % CPU | Nice | ID    | Memory    | Waiting Channel
MyProgramName | Sleeping |   0   |  0   | 28573 | 300.0 KiB | futex_wait_queue_me

The multithreading part of my project was written by someone else, and there is no documentation. So I'm kind of getting stuck here.

Please help me out.

If you need more information, please let me know. I will try my best to add.

And here is my main() logic:

MyServer server(host, port); // When `return -1` is called, this line will eventually call `pthread_join (tid, 0)`
try
{
    server.init(config);
}
catch (const MyException& e)
{
    DebugUtils::Error(boost::format("Error initializing server: %1% [%2%].")
                      % e.what() % e.GeneralMessage);
    return -1;
}
catch (const std::exception& e)
{
    DebugUtils::Error(boost::format("Error initializing server: %1%.")
                      % e.what());
    return -1;
}
catch (...)
{
    DebugUtils::Error("Unknown error initializing server.");
    return -1;
}

try
{
    server.start();
}
catch (const MyException& e)
{
    DebugUtils::Error(boost::format("Error starting server: %1% [%2%].")
                      % e.what() % e.GeneralMessage);
    return -1;
}
catch (const std::exception& e)
{
    DebugUtils::Error(boost::format("Error starting server: %1%.")
                      % e.what()); // This line output the error info
    return -1; // This line will first free the resource, so it will call the destructor of MyServer on the first line in this code snippet.
}
catch (...)
{
    DebugUtils::Error("Unknown error starting server.");
    return -1;
}

Peter

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

時窥 2024-12-02 00:49:37

我怀疑您需要发出条件信号或向事件队列添加一个项目来唤醒子线程,以便它可以在执行 pthread_join 调用之前退出。

I suspect you need to signal a condition or add an item to an event queue to wake up the sub-thread so it can exit before you do the pthread_join call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文