Re: [gnet] Question about gnet and g_main_loop_run
- From: Kostas Katsamakas <kmak ccf auth gr>
- To: gnet lists gnetlibrary org
- Subject: Re: [gnet] Question about gnet and g_main_loop_run
- Date: Thu, 21 Jul 2005 13:06:30 +0300
Tim Müller wrote:
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
_______________________________________________
gnet mailing list
gnet lists gnetlibrary org
http://lists.gnetlibrary.org/mailman/listinfo/gnet
Yes you are correct about me not running a main_loop. I instead use like
you said something like this:
while (stay_alive) {
g_main_context_iteration (NULL, false);
}
The problem with the main loop was this. After i run the main loop
any gnet_server_new i create can't receive events (connect events don't come)
If i first create the server and then run the main loop then i can receive
new connections and error events on the server.
So i had to do something like the above code. I also tried
g_main_context_iteration (NULL, true);
but it behaves like the main loop, the events don't happen.
Can't find any way out of this. Perhaps i should try
creating the servers before the main loop runs. That sounds
too restrictive though.
Thanks again
Kostas
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]