Re: [Vala] Threads and async (was: Threads and closures problem)



Ok, I just tried the following program:

=================================================
void* thread_func_1()
{
        var loop = new MainLoop();
        async_func_1();
        loop.run();
        return null;
}

void* thread_func_2()
{
        var loop = new MainLoop();
        async_func_2();
        loop.run();
        return null;
}

async void async_func_1()
{
        message( "thread %d", (int)Linux.gettid() );
        Thread.usleep( 1000 * 1000 );
        yield async_func_1();
}

async void async_func_2()
{
        message( "thread %d", (int)Linux.gettid() );
        Thread.usleep( 1000 * 1000 );
        yield async_func_2();
}

void main()
{
        Thread.create( thread_func_1, false );
        Thread.create( thread_func_2, false );
        while ( true )
        {
                message( "main thread %d", (int)Linux.gettid() );
                Thread.usleep( 1000 * 1000 * 2 );
        }
}
=================================================

And to my amazement, the output looked like that:

** Message: thread.vala:37: main thread 12070
** Message: thread.vala:19: thread 12071
** Message: thread.vala:26: thread 12072
** Message: thread.vala:19: thread 12071
** Message: thread.vala:26: thread 12072
** Message: thread.vala:37: main thread 12070
** Message: thread.vala:19: thread 12071
** Message: thread.vala:26: thread 12072
** Message: thread.vala:19: thread 12071
** Message: thread.vala:26: thread 12072

Reading a bit further into the glib docs, it clearly states that the
"default" context is thread specific.

Very good,

:M:





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