* condition they signal the #GCond, and that causes the waiting
* threads to be woken up.
*
+ * Consider the following example of a shared variable. One or more
+ * threads can wait for data to be published to the variable and when
+ * another thread publishes the data, it can signal one of the waiting
+ * threads to wake up to collect the data.
+ *
* <example>
* <title>
* Using GCond to block a thread until a condition is satisfied
* </title>
* <programlisting>
- * GCond* data_cond = NULL; /<!-- -->* Must be initialized somewhere *<!-- -->/
- * GMutex* data_mutex = NULL; /<!-- -->* Must be initialized somewhere *<!-- -->/
* gpointer current_data = NULL;
+ * GMutex data_mutex;
+ * GCond data_cond;
*
* void
* push_data (gpointer data)
* {
- * g_mutex_lock (data_mutex);
+ * g_mutex_lock (&data_mutex);
* current_data = data;
- * g_cond_signal (data_cond);
- * g_mutex_unlock (data_mutex);
+ * g_cond_signal (&data_cond);
+ * g_mutex_unlock (&data_mutex);
* }
*
* gpointer
* {
* gpointer data;
*
- * g_mutex_lock (data_mutex);
+ * g_mutex_lock (&data_mutex);
* while (!current_data)
- * g_cond_wait (data_cond, data_mutex);
+ * g_cond_wait (&data_cond, &data_mutex);
* data = current_data;
* current_data = NULL;
- * g_mutex_unlock (data_mutex);
+ * g_mutex_unlock (&data_mutex);
*
* return data;
* }
* current_data is non-%NULL, i.e. until some other thread
* has called push_data().
*
- * <note><para>It is important to use the g_cond_wait() and
- * g_cond_timed_wait() functions only inside a loop which checks for the
- * condition to be true. It is not guaranteed that the waiting thread
- * will find the condition fulfilled after it wakes up, even if the
- * signaling thread left the condition in that state: another thread may
- * have altered the condition before the waiting thread got the chance
- * to be woken up, even if the condition itself is protected by a
- * #GMutex, like above.</para></note>
+ * The example shows that use of a condition variable must always be
+ * paired with a mutex. Without the use of a mutex, there would be a
+ * race between the check of <varname>current_data</varname> by the
+ * while loop in <function>pop_data</function> and waiting.
+ * Specifically, another thread could set <varname>pop_data</varname>
+ * after the check, and signal the cond (with nobody waiting on it)
+ * before the first thread goes to sleep. #GCond is specifically useful
+ * for its ability to release the mutex and go to sleep atomically.
+ *
+ * It is also important to use the g_cond_wait() and g_cond_wait_until()
+ * functions only inside a loop which checks for the condition to be
+ * true. See g_cond_wait() for an explanation of why the condition may
+ * not be true even after it returns.
*
* If a #GCond is allocated in static storage then it can be used
* without initialisation. Otherwise, you should call g_cond_init() on
* GThread:
*
* The #GThread struct represents a running thread. This struct
- * is returned by g_thread_new() or g_thread_new_full(). You can
- * obtain the #GThread struct representing the current thead by
- * calling g_thread_self().
+ * is returned by g_thread_new() or g_thread_try_new(). You can obtain
+ * the #GThread struct representing the current thead by calling
+ * g_thread_self().
*
* The structure is opaque -- none of its fields may be directly
* accessed.
* GThreadFunc:
* @data: data passed to the thread
*
- * Specifies the type of the @func functions passed to
- * g_thread_new() or g_thread_new_full().
+ * Specifies the type of the @func functions passed to g_thread_new() or
+ * g_thread_try_new().
*
* Returns: the return value of the thread
*/
* a debugger. Some systems restrict the length of @name to
* 16 bytes.
*
- * @error can be %NULL to ignore errors, or non-%NULL to report errors.
- * The error is set, if and only if the function returns %NULL.
+ * If the thread can not be created the program aborts. See
+ * g_thread_try_new() if you want to attempt to deal with failures.
*
- * You must
- *
- * Returns: the new #GThread, or %NULL if an error occurred
+ * Returns: the new #GThread
*
* Since: 2.32
*/
GThread *
-g_thread_new (const gchar *name,
- GThreadFunc func,
- gpointer data,
- GError **error)
+g_thread_new (const gchar *name,
+ GThreadFunc func,
+ gpointer data)
{
- return g_thread_new_internal (name, g_thread_proxy, func, data, 0, error);
+ GError *error = NULL;
+ GThread *thread;
+
+ thread = g_thread_new_internal (name, g_thread_proxy, func, data, 0, &error);
+
+ if G_UNLIKELY (thread == NULL)
+ g_error ("creating thread '%s': %s", name ? name : "", error->message);
+
+ return thread;
}
/**
- * g_thread_new_full:
+ * g_thread_try_new:
* @name: a name for the new thread
* @func: a function to execute in the new thread
* @data: an argument to supply to the new thread
- * @stack_size: a stack size for the new thread
- * @error: return location for error
- *
- * This function creates a new thread. The new thread starts by
- * invoking @func with the argument data. The thread will run
- * until @func returns or until g_thread_exit() is called.
- *
- * The @name can be useful for discriminating threads in
- * a debugger. Some systems restrict the length of @name to
- * 16 bytes.
+ * @error: return location for error, or %NULL
*
- * If the underlying thread implementation supports it, the thread
- * gets a stack size of @stack_size or the default value for the
- * current platform, if @stack_size is 0. Note that you should only
- * use a non-zero @stack_size if you really can't use the default.
- * In most cases, using g_thread_new() (which doesn't take a
- * @stack_size) is better.
+ * This function is the same as g_thread_new() except that
+ * it allows for the possibility of failure.
*
- * @error can be %NULL to ignore errors, or non-%NULL to report errors.
- * The error is set, if and only if the function returns %NULL.
+ * If a thread can not be created (due to resource limits),
+ * @error is set and %NULL is returned.
*
* Returns: the new #GThread, or %NULL if an error occurred
*
* Since: 2.32
*/
GThread *
-g_thread_new_full (const gchar *name,
- GThreadFunc func,
- gpointer data,
- gsize stack_size,
- GError **error)
+g_thread_try_new (const gchar *name,
+ GThreadFunc func,
+ gpointer data,
+ GError **error)
{
- return g_thread_new_internal (name, g_thread_proxy, func, data, stack_size, error);
+ return g_thread_new_internal (name, g_thread_proxy, func, data, 0, error);
}
GThread *
* g_thread_self:
*
* This functions returns the #GThread corresponding to the
- * current thread.
+ * current thread. Note that this function does not increase
+ * the reference count of the returned object.
*
* Returns: the #GThread representing the current thread
*/