Re: [Vala] Thread Support



Thanks for your comments and suggestions.


2014-08-13 14:02 GMT-05:00 Jim Nelson <jim yorba org>:


On Wed, Aug 13, 2014 at 10:55 AM, Daniel Espinosa <esodan gmail com>
wrote:

I have an async function called directly in my code. When compiles I get
this: XXXXXXX: warning: implicit .begin is deprecated


That means you called the async method with neither a yield keyword nor
with the begin modifier.  You need to do either this:

yield read_async();


This form is not documented in Vala Manual. I'll add it. This seems to be
more convenient for reading, the second could be read as "an object
read_async have a method called begin() and it is called", but the first
means: "you call a function read_async() and yields it up to finish it".
I'll use the first form in my code.



or this:

read_async.begin();


For the comment above, I think this form must be deprecated to add clear
meaning in Vala code for async functions, even the compiler must rise an
error when yield is used in unasync function.



Vala used to support:

read_async()

as being equivalent to using .begin(), but that form was deprecated, hence
the message you're seeing.

Thread<void> th = new Thread<void>.try ("loading scl", () => {
async_function (); /*Asinc function */ return null; }); th.join (); Error
message is: XXXXX: error: The name `try' does not exist in the context of
`GLib.Thread' I've checked glib-2.0.vapi, for both vala-0.20 and 0.22, that
function is present just for GLIB_2_32, checking my installed version I
have a 2.40 on a GNOME Ubuntu 14.04 box.


Add this flag to valac:

--target-glib=2.40

or:

--target-glib=2.32

Be aware by doing this you're making your app incompatible with earlier
versions of glib.


Then this is a bug in glib-2.0.vapi, because it seds:

[Deprecated (since = "2.32", replacement = "new Thread<T> ()")]
[CCode (simple_generics = true)]
public static unowned Thread<T> create<T> (ThreadFunc<T> func, bool
joinable) throws ThreadError;

Then this makes compiler to throws a warning each time you don't use
--target-glib=2.32 or some thing, and this is a mistake because you are
using "generic" code, if I use Thread<T>.try I'm making back incompatible
code.

If I follow compiler warning to replace deprecated code, I found the
problem above, then defining GLIB_2_32 must be present by default or even
better modify to glib-2.0.vapi to help the developer how to proceed:

[Deprecated (since = "2.32", replacement = "new Thread<T> () and valac's
--target-glib=2.32 flag")]
[CCode (simple_generics = true)]
public static unowned Thread<T> create<T> (ThreadFunc<T> func, bool
joinable) throws ThreadError;




Regarding your code itself, there's a few more problems.  First, you can't
use <void> as a type parameter, you must give an actual type that
represents storage.  "void *" is probably the best way to go if you truly
have nothing to pass to your threads.


Your explanation is a good point to be added to Vala's Manual.



Second, you're calling an async method within the background thread.
 There's nothing technically wrong with this, but I suspect that's not what
you really want.  The usual pattern of use is to call the async method from
your main ("foreground") thread.  The async call might use background
threads as an implementation strategy (to do the work without blocking the
foreground thread).

-- Jim


You're right, I've changed to

async_function.begin ()

and I've removed Thread, but now I'll use

yield async_function().

for better understanding.

Warning from compiler must suggest to use this instead to just warn about
use implicit .begin is deprecated. Message could be:

"async functions should use attribute .begin or 'yield' modifier when it is
called"

-- 
Trabajar, la mejor arma para tu superación
"de grano en grano, se hace la arena" (R) (en trámite, pero para los
cuates: LIBRE)


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