[Vala] fixed threading example



By answering Thij's bug report..here's the fixed version for threads found
in:

http://bugzilla.gnome.org/show_bug.cgi?id=534828

Is this the proper way to use function pointers? the example in
documentation makes use of the deprecated "pointer" type. I have replaced
it with 'void*', but the need to cast the ThreadFunc looks ugly to me.

btw i have noticed that thread_create takes only two arguments:

  "public static weak Thread create (ThreadFunc func, bool joinable)
throws ThreadError;"

Are the threads in glib not receiving any data pointer as first argument
while creating them? pthread ones does it in this way and its quite
useful.

the definition in glib/gthread.h takes 4 arguments:

-------------
#define g_thread_create(func, data, joinable, error)                    \
  (g_thread_create_full (func, data, 0, joinable, FALSE,                \
                         G_THREAD_PRIORITY_NORMAL, error))

GThread* g_thread_create_full  (GThreadFunc            func,
                                gpointer               data,
                                gulong                 stack_size,
                                gboolean               joinable,
                                gboolean               bound,
                                GThreadPriority        priority,
                                GError               **error);
-------------

so..the example below works. but is imho not correct, or at least not
complete.


using GLib;

public class Threading : GLib.Object {

    public static void thread_func(void *data) {
        stdout.printf("Thread running.\n");
    }

    public static void main(string[] args) {
        if (!Thread.supported()) {
            stderr.printf("Cannot run without threads.\n");
            return;
        }

        try {
            Thread.create((GLib.ThreadFunc)thread_func, true);
        } catch (ThreadError ex) {
            return;
        }
    }
}

--pancake



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