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

2010-01-13 Thread Jiří Zárevúcky
Michael 'Mickey' Lauer píše v Čt 14. 01. 2010 v 00:48 +0100:
 Jörn's mail about threads and closures reminded me about a problem I
 wanted to highlight here.
 
 The addition of async functions in Vala is a very great feature, however
 I wonder how this could work with multiple threads? As far as I know,
 the continuation is scheduled to be an idle callback against the main
 context, right? If so, then this would mean that an async functions in a
 thread B would only run in the B-context until the first 'yield',
 afterwards it would continue to run in the A-context -- obviously
 defeating the purpose of multithreading.

Async methods exist for us to be able to write coroutines without
multithreading. When you use multiple threads, you don't need async at
all.

 
 Are my worries justified and if so, how could we fix that?
 

No, I don't think they are.

 Cheers,
 




signature.asc
Description: Toto je digitálně  podepsaná část  zprávy
___
Vala-list mailing list
Vala-list@gnome.org
http://mail.gnome.org/mailman/listinfo/vala-list


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

2010-01-13 Thread Michael 'Mickey' Lauer
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:


___
Vala-list mailing list
Vala-list@gnome.org
http://mail.gnome.org/mailman/listinfo/vala-list


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

2010-01-13 Thread Michael 'Mickey' Lauer
Looks like I spoke too soon. The attached program emits a wrong output:

** Message: thread.vala:43: main thread 14350
** Message: thread.vala:31: thread 14352
** Message: thread.vala:21: thread 14351
** Message: thread.vala:31: thread 14352
** Message: thread.vala:21: thread 14352
** Message: thread.vala:31: thread 14352
** Message: thread.vala:21: thread 14352
** Message: thread.vala:43: main thread 14350
** Message: thread.vala:31: thread 14352
** Message: thread.vala:21: thread 14352


-- 
:M:
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()
{
	while ( true )
	{
	message( thread %d, (int)Linux.gettid() );
	Timeout.add_seconds( 1, async_func_1.callback );
	yield;
	}
}

async void async_func_2()
{
	while ( true )
	{
	message( thread %d, (int)Linux.gettid() );
	Timeout.add_seconds( 1, async_func_2.callback );
	yield;
	}
}

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 );
	}
}
___
Vala-list mailing list
Vala-list@gnome.org
http://mail.gnome.org/mailman/listinfo/vala-list