Synchronously waiting for finalizing GSourceFunc added by g_idle_add()



Dear readers!

Trying to avoid calling to gtk functions from threads, I'm facing a data
race problem. All I need is to get from thread some data, entered by a user.
Based on Example 6 "Using GCond to block a thread until a condition is
satisfied" it looks like:

gpointer current_data = NULL;
GMutex data_mutex;
GCond data_cond;

gboolean
push_data (gpointer data)
{

  // ...
  result = gtk_dialog_run(GTK_DIALOG(dialog));
  user_data = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));

  g_mutex_lock (&data_mutex);
  current_data = user_data;
  g_cond_signal (&data_cond);
  g_mutex_unlock (&data_mutex);       //  (1)

  return FALSE;
}

gpointer
pop_data (void)
{
  gpointer data;
  guint id;

  id = g_idle_add(push_data, NULL);
  // id = gdk_threads_add_idle(push_data, NULL);

  g_mutex_lock (&data_mutex);
  while (!current_data)
    g_cond_wait (&data_cond, &data_mutex);
  data = current_data;
  current_data = NULL;
  g_mutex_unlock (&data_mutex);

  // All resources must be released here,
  // but the call (1) can be still not finished
  // and even if it would be, the processing of push_data() from main loop
can be still in action.
  //
  // Is there any approach like:
  // g_source_join(id); // ???

  return data;
}

Thanks,
Andrey



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