Re: [Vala] How to check if current thread is main thread?



On Fri, 2012-07-06 at 08:11 +0200, marcin saepia net wrote:
Hi

I need to check (for debugging purposes) if current thread is main
thread. How can I do that in Vala?

I know that in GLib exists g_thread_self() and I can  use result of
that function for that purpose but in Vala's vapi it has strange
syntax that I cannot truly understand.

Thank you in advance,

m.

I know that this is a very old email :)

Just for future reference:

using GLib;

public class ThreadSelf {
        private unowned Thread<void*> main_thread;
        
        public ThreadSelf () {
                main_thread = Thread.self<void*> ();
                unowned Thread<void*> thread = Thread.create<void*> (this.fun, false);
                Thread.usleep (100);
        }
        
        public void* fun () {
                stderr.printf("ThreadSelf.fun - running in main thread: %s\n",
Thread.self<void*>() == main_thread ? "yes" : "no");
                return null;
        }
        
        public static void main () {
                var thread = new ThreadSelf();
                thread.fun ();
        }
}

HTH,
Andrea




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