[Vala] Question re vala async example

The code pasted below is from http://live.gnome.org/Vala/AsyncSamples.

My query is: is the code safe? 
What will happen if the thread runs to completion
before the main thread ever executes the yield?

The reason I'm asking is that currently I'm using a rather
ugly way to wait (Timeout and yield inside a while(somevar))
and the code below looks better. But I'm rather
concerned about its safety.

async double do_calc_in_bg(double val) throws ThreadError {
    SourceFunc callback = do_calc_in_bg.callback;
    double[] output = new double[1];

    // Hold reference to closure to keep it from being freed whilst
    // thread is active.
    ThreadFunc<void*> run = () => {
        // Perform a dummy slow calculation.
        // (Insert real-life time-consuming algorithm here.)
        double result = 0;
        for (int a = 0; a<10000000; a++)
            result += val * a;

        // Pass back result and schedule callback
        output[0] = result;
        Idle.add((owned) callback);
        return null;
    Thread.create<void*>(run, false);

    // Wait for background thread to schedule our callback
    return output[0];

Nor Jaidi Tuah

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