Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

multi threaded script



Hi,

I wanted to write a script that needs to be multithreaded, for example
an UI in zsh/curses that does things in background while the UI is
still responsive.
As I found nothing to be able to execute parallelize processes in zsh
(I'm not speaking of fork()) I began to wrote a small and dirty
pthread module for zsh it was quite easy, currently I only implemented
:
pthread_create, pthread_join, pthread_mutex_lock pthread_mutex_unlock,

As soon as I tryed to use it some problems occurs which shows me (in
fact IRC guys show this to me :)) that pthreads is not a good idea
because of global variables.

So my question as a zsh/zthread module won't make sense in zsh, what
would make sense to a able to achieve this stupid example :


zmodload zsh/zthread
zmutex init lock1
integer toto
toto=11

thread1() {
    zmutex lock lock1
    print "Hello from thread1 $toto"
    toto+=1
    zmutex unlock lock1
    sleep 5
    zmutex lock lock1
    print "again thread1 $toto"
    zmutex unlock lock1
    return
}
thread2() {
    zmutex lock lock1
    print "Hello from thread2 $toto"
    toto+=2
    zmutex unlock lock1
    sleep 5
    zmutex lock lock1
    print "again thread2 $toto"
    zmutex unlock lock1
    return
}
zthread create thr1 thread1
zthread create thr2 thread2

zthread join thr1
zthread join thr2

for information zthread sometime works with this, sometimes shows
garbage, sometimes it exit with pthread fatal error.

Perhaps it is not a good idea to use zsh for that kind of purpose.

regards,
Bapt



Messages sorted by: Reverse Date, Date, Thread, Author