Re: Delay time to spawn new threads?
- From: David Buchan <pdbuchan yahoo com>
- To: Chris Vine <chris cvine freeserve co uk>
- Cc: gtk-app-devel-list list <gtk-app-devel-list gnome org>
- Subject: Re: Delay time to spawn new threads?
- Date: Wed, 27 Nov 2013 19:32:26 -0800 (PST)
David Buchan <pdbuchan yahoo com> wrote:
Hi Michael,
My 32-bit, GTK+2 version does
// Secure glib
if (!g_thread_supported ()) {
g_thread_init (NULL);
}
at the beginning, and then the thread is spawned via:
on_button1_clicked (GtkButton *button1, MyData *data)
{
GThread *thread;
GError *error = NULL;
thread = g_thread_create ((GThreadFunc) my_function, data, FALSE,
&error); if (! thread) {
g_print ("Error: Unable to create new thread for my_function()
in on_button1_clicked().%s\n", error->message); exit (EXIT_FAILURE);
}
My 64-bit, GTK+3 versions does not do the g_thread_init() call.
It spawns a new thread via:
int
on_button1_clicked (GtkButton *button1, MyData *data)
{
GThread *thread;
thread = g_thread_new ("my_function", (GThreadFunc) my_function,
data); if (! thread) {
fprintf (stderr, "Error: Unable to create new thread for
my_function() in on_button1_clicked().\n"); exit (EXIT_FAILURE);
}
Show us your my_function(): you are almost certainly doing something
wrong. Best of all, provide a complete compilable example which
demonstrates the problem. And why are you casting the function pointer
to GThreadFunc? You do not need to call g_thread_init() with glib >=
2.32, and you do with earlier versions. Prior to version 2.24
g_thread_init() had to be the first glib call. Between 2.24 and 2.30 it
had to be the first
call relevant to threads.
Also, please don't top post.
Chris
==========================================
Hi Chris,
I removed the call to g_thread_init() and it still works fine! Great catch there.
I had put the cast to GThreadFunc there because otherwise I get the following error:
callbacks.c: In function ‘on_button1_clicked’:
callbacks.c:3829: warning: passing argument 1 of ‘g_thread_create_full’ from incompatible pointer type
/usr/include/glib-2.0/glib/gthread.h:225: note: expected ‘GThreadFunc’ but argument is of type ‘int
(*)(struct MyData *)’
I saw other people on the web do the cast as well to remove that error. Is there a mistake in how I use
g_thread_create()? I'd like to drop the cast if I can. It seemed strange at the time I put it in.
Top-posting is due to the way Yahoo mail handles replies.
Maybe this
is better, but it didn't put the >'s in, thus the "====..." line. I dunno. Sorry about that.
I'm still investigating the delay. The my_function() code is hundreds of lines, so I'd need to create a small
example to post.
Let me continue to investigate.
Dave
==========================
... and now I see what's going on.
My newly spawned thread calls a function which starts a messaging idle-function.
[my main program] ---> [my new thread] ----> [post_message function] ===> [update GUI idle function]
In my new thread, I call:
post_message (text, data);
and that looks like:
// Start an instance of activity_message() idle function.
int
post_message (char *message, MyData *data)
{
// Allocate memory for string; Idle function will free it.
data->activity_message = allocate_strmem (TEXTLEN);
// Copy message.
strncpy (data->activity_message, message, TEXTLEN - 1); // Minus 1 for string termination.
g_idle_add ((GSourceFunc) activity_message, data);
sleep (1);
return (EXIT_SUCCESS);
}
// Idle function to add a message to the activity textview
// This idle function returns 0 in order to stop.
int
activity_message (MyData *data)
{
...
Update a textview with our message ...
return (0)
}
I set it up this way so the GUI can be updated.
The call to sleep() protects me against the situation in which my thread makes a second call to
post_message() when activity_message() hasn't finished updating the GUI from the first call.
Is there a way to have my thread call post_message() whenever it needs to, and have the messages queue-up?
Dave
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]