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




As I said, using pointers did cross my mind, but I have a problem on how and when to properly release the resources.

Can you supply some tested pattern?

I was thinking of adding a "completed" signal in MyThread and fire it at the end of thread func, then in a callback, delete the MyThread  instance... But this is *extremely* unrobust solution...Is there a way (in glib) to know when the new tread finishes?

10x
MihailNaydenov

>----- Original Message ----
>From: Florian Brosch <flo brosch gmail com>
>To: Mihail Naydenov <mlists ymail com>
>Sent: Sunday, September 28, 2008 5:13:43 PM
>Subject: Re: [Vala] GPrivete and GStaticPrivate...and threads with their own data

>Just use pointers for manual memory management to create the class, or hold the reference to the class somewhere else outside the function.


2008/9/28 Mihail Naydenov <mlists ymail com>
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





_______________________________________________
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]