Re: Delay time to spawn new threads?






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


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]