Re: [Vala] fixed threading example



Answers down the page...

2008/5/26 pancake <pancake youterm com>:
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);

The C version compared to the Vala version:
func -> the method delegate (a ThreadFunc)
data -> the class instance that the method belongs to (ThreadFunc is
not a static delgate)
joinable -> joinable, same thing
error -> throws ThreadError

I think that all works out ok.  You can't pass arbitrary data, but
that isn't the pattern that Vala bindings usually follow anyway.
Instead you pass an instance method and a pointer to the instance goes
automatically.

-------------

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

Actually, thread_func doesn't have quite the right signature, but
that's about right.  I'll fix up the tutorial, thanks for pointing out
the problem.

-- 
Phil Housley



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