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



Thank you, for your response!
I have tested that solution and it does not work when the tread is created within a function, because the MyThread class
is destroyed at the end of that function and it (and its data) cannot be accessed from the other thread.

---In my case:
  public bool on_key_press (Stage sender, KeyEvent e)
  {
    if (e.keyval == 's')
    {
      var worker = new MyThread ();
      worker.bar = this.bar;

      GLib.Thread.create (worker.func, false);

    //***here the worker is destroyed ***
    //(worker == NULL ? NULL : (worker = (worker_unref (worker), NULL)));   
    }
    return true;
  }

---At runtime:
CRITICAL **: worker_func: assertion `IS_WORKER (self)' failed

Or many more scary things is class MyThread is defined as [Compact]!!!

Is there a way to work around this?
Once again, Im a novice, sorry if I do not get something basic.

10x once again
MihailNaydenov

PS Of course I can always make worker part of the calling class, but the point is to create arbitrary number of new threads on the fly...
PPS I may use pointer to worker... but right now I really do not know were and how I will release the memory myself...


----- Original Message ----
From: Frederik <scumm_fredo gmx net>
To: vala-list gnome org
Sent: Sunday, September 28, 2008 12:00:37 AM
Subject: 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
_______________________________________________
Vala-list mailing list
Vala-list gnome org
http://mail.gnome.org/mailman/listinfo/vala-list






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