开发者

Frustrated with my Program Hanging on Return with Waiting Channel of futex_wait_queue_me

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);
}
ca开发者_开发问答tch (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 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜