JM <[email protected]> writes:

> Hi all
> I just played around with closures as thread functions. I'm not sure if
> this example is supposed to work, but at least it compiles.
>
>
> class HHH : Object {
>       public void run() {
>               string test = "test";
>               try {
>                       Thread.create( ()=> { print("in thread : %s \n", test); 
> }, false);

Hi

if you look into a hhh.c generated with

valac -C --thread hhh.vala

you can see that the clause data is destroyed at the end of HHH.run
(hhh_run in C) as the closure function is no longer referenced when
exiting HHH.run, you have to assign the closure to a variable to keep
it alive, for example

class HHH : Object {
        ThreadFunc f;
        public void run() {
                string test = "test";
                try {
                    f = ()=> { Thread.usleep(1000); print("in thread : %s \n", 
test); };
                    Thread.create(f, false);

it gives a compilation warning but works (I needed to add usleep to observe 
your problem)

$ valac --thread --pkg posix temp.vala
hhh.vala.c: In function ‘hhh_run’:
hhh.vala.c:111: warning: assignment from incompatible pointer type

>
> Anybody knows how this should be handled?
> Regards
> Jörn
_______________________________________________
Vala-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/vala-list

Reply via email to