Re: [Vala] Threads and closures problem



JM <interflug1 gmx net> 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



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