Register to post in forums, or Log in to your existing account

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion

Joined: 21 Oct 2000
Posts: 821
Location: Australia

PostPosted: Tue Jun 09, 2009 12:02 am   

threading questions/problems
I only partially understand threading in CMUD. As I understand it though, if I make a script run as a separate thread, then other operations of my system will not wait on the completion of this script. I have read the help files, but from what I can tell, the only way for me to force this script into its own thread it to suspend and then resume it again.

This being my main curing alias for an IRE mud, if I place
#wait 1
before I begin my large looping structure, then the script will be suspended for 1 millisecond before resuming parallel to my ordinary scripts.

I have tested this by placing an #UNTIL loop in a test alias, with an expression that can never evaluate to true. It becomes impossible for me to enter anything at the command line, and lines received from the mud are not displayed. If I hit ESC, this terminates the loop.

I then placed #wait 1 in the alias, just before the #until command. Running the alias does not cause a lockup, so I can only assume that it has worked and the badly written #until script is now running in the background, as its own thread.

Problem 1:
Entering #THREAD at the command line should list this thread separately to the main system thread, right?

It names this thread the same name as the main system thread, which is to say the name of :my main session window. It does display it with a different thread number, but it is difficult to tell which script belongs to which thread

# ID Window Name Status Script
2 [u] IrruSentaari stopped
4 [u] IrruSentaari running
5 [u] IrruSentaari running

Is there some way I can specify an ID/name for this thread? I know the ID means alarm IDs, but the above looks like the main system thread has been stopped somehow and for some reason.

Question 2
I've read that I need to be careful about threading and shared resources. The #section command. I'm a little confused, but I think that my thread will be okay.

The alias first defines a whole lot of local variables. My list of current afflictions, for example:

By the time I begin the #until loop, there are no calls to global variables, which should mean it is thread-safe - right?

However, if I were to make a call to a global variable, via a function, perhaps something like:

#func balcheck {
#IF (@herbb && @pherbb) {$x=1}
#return $x}

Do I need to use the #section command somewhere, and if so, where?

Question 3
If I run this alias twice in a row, quickly - is there a way I can detect whether the original script is still running in its own thread or not, and if so, to terminate the original thread, and start it again rather than ending up with two identical threads running side by side?
Athlon 64 3200+
Win XP Pro x64
Reply with quote
Fang Xianfu

Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Tue Jun 09, 2009 11:40 am   
1) Use #thread. You can specify a name for the thread at the same time you push the script into its own thread, so you won't need the #wait if you go that route either.
2) Threading problems come about because the values can change at any time during the script's execution. You need to be careful when setting global variables, yes, but you also need to be careful if, for example, the value of @herbb might change between when it's evaluated and when $x=1 is sent. In some scripts that can cause errors.
3) Vijilante does this a lot, you might want to check his toolbox in the library. I can't recall how he does it, though.
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote

Joined: 21 Oct 2000
Posts: 821
Location: Australia

PostPosted: Tue Jun 09, 2009 3:19 pm   
Hrm, thanks for the reply. I'll test it out tomorrow, but its looking like

#thread "cure_aff" {
#until (blah blah) { ..... }

will do exactly what I want.
Athlon 64 3200+
Win XP Pro x64
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by