Re: [Vala] GPrivete and GStaticPrivate...and threads with their own data



Mihail Naydenov wrote:
After same digging I came to this:
http://mail.gnome.org/archives/vala-list/2008-May/msg00144.html

It seams there is no way to pass custom data to new threads, they always operate on the spawning class members.

I do not know much about threading and vala, but that is quite limiting, and even the simplest things (like giving the new thread a unique name) look hard or impossible in my head :/ Even events have both Sender and Event args to represent the context in which they are called...

Please, anyone with vala threading experience, share the typical pattern of using threads,.
Many thanks in advance!
MihailNaydnov

Hi,

you could encapsulate your thread data in a class:

-----------------------

class MyThread {

    private string name;
    private int count = 0;

    public MyThread (string name) {
        this.name = name;
    }

    public void* thread_func () {
        while (true) {
            stdout.printf ("%s: %i\n", this.name, this.count);
            this.count++;
            Thread.yield ();
        }
        return null;
    }
}

static int main (string[] args) {
    if (!Thread.supported ()) {
        error ("Cannot run without threads.");
        return -1;
    }

    var t1 = new MyThread ("A");
    var t2 = new MyThread ("B");
    weak Thread thread1;
    weak Thread thread2;

    try {
        thread1 = Thread.create (t1.thread_func, true);
        thread2 = Thread.create (t2.thread_func, true);
    } catch (ThreadError ex) {
        return -1;
    }

    thread1.join ();
    thread2.join ();

    return 0;
}

-----------------------

Regards



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