Re: [gnet] Question about gnet and g_main_loop_run
- From: Tim Müller <t i m zen co uk>
- To: gnet gnetlibrary org
- Cc:
- Subject: Re: [gnet] Question about gnet and g_main_loop_run
- Date: Thu, 21 Jul 2005 10:27:39 +0100
On Thursday 21 July 2005 07:30, Kostas Katsamakas wrote:
> I am using gnet in a windows application. I create a thread which
> runs g_main_loop_run(); In my main application thread i create
> the server by calling gnet_server_new().
>
> The problem is that nothing works if i create the server after
> g_main_loop_run has been executed. It is important that i can
> create a server at any time, not only before g_main_loop()
>
> I have found a way to overcome this. If instead of calling
> g_main_loop_run() in my second thread i call
>
> g_main_context_iteration (context, false); //See if any events to poll
>
> in a loop then everything works fine. The problem in this
> solution is active waiting. The cpu usage reaches 99%
>
> I wonder if anyone could help me with this. I would like to be able
> to create a new server anytime but still without the cpu usage cost.
I'm not entirely sure I understand what you're doing, but here's how it should
work:
GServer automatically adds a hook to the default GLib main context (context =
NULL) that makes sure everything works asynchroneously and non-blocking. This
only works if you're running a main loop (in the default context), like
gtk_main() for example.
From your description I take it that you are not running a main loop, and that
is why you create a thread where you do something like
while (stay_alive) {
g_main_context_iteration (NULL, false);
}
? That will definitively lead to 99% CPU usage. After all, if there's nothing
to do (which is almost always the case), it will return immediately, which
will check again right away, which ...
Have you tried one of these:
while (stay_alive) {
g_main_context_iteration (NULL, true);
}
which will block until something happens (downside: you need to explicitely
make it return when you unset stay_alive), or
while (stay_alive) {
while (stay_alive && g_main_context_pending (NULL)) {
g_main_context_iteration (NULL, false);
}
g_usleep (G_USEC_PER_SEC/20); /* sleep ca. 50ms */
}
Cheers
-Tim
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]