Re: [Vala] async method and delegate

With async, the delegate *is* copied, you just don't see it in the Vala
code.  With an async method, when the thread of execution yields, the state
of the function at that point is stored (copied or ref'd) in a context
structure.  When the thread of execution resumes later, the state is pulled
back out and the function resumes.

To see this, try examining the C code valac generates (use the --save-temps
argument).  You'll see in the async version of the f2() function an F2Data
struct being allocated and all the arguments being stuffed into it.  That's
where the delegate is being copied.

-- Jim

On Mon, Jun 20, 2011 at 5:26 PM, Nor Jaidi Tuah <norjaidi tuah ubd edu bn>wrote:

On Mon, 2011-06-20 at 13:06 -0700, Jim Nelson wrote:
Regarding copying delegates, it is intentional.  The Vala Journal has
a nice write-up about ways to work around the problem:

This is not about copying delegates. Consider the
example in the vala tutorial:

 delegate void DelegateType(int a);

 void f1(int a) {
     stdout.printf("%d\n", a);

 void f2(DelegateType d, int a) {
     d(a);       // Calling a delegate

 void main() {
     f2(f1, 5);

This example compiles with no problem. But if we make
f2 async, i.e.,

 async void f2(DelegateType d, int a) {
     d(a);       // Calling a delegate

the warning appears.

Nor Jaidi Tuah

vala-list mailing list
vala-list gnome org

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