Re: [Vala] Threads and closures problem
- From: lukpank o2 pl (Łukasz Pankowski)
- To: JM <interflug1 gmx net>
- Cc: vala-list gnome org
- Subject: Re: [Vala] Threads and closures problem
- Date: Thu, 14 Jan 2010 00:38:56 +0100
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]