LSL Wiki Mirror 7-7-7 : multithreading

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings ::

Multithreading


Multithreading can be used to pass delayed tasks to other scripts by link messages.

Warning: The practice of multithreading to eliminate script delays is a mildly dangerous one at best, and holds the potential to bring a server to its knees, or outright crash it at its worst. Please, for the love of the hippos, try to refrain from multithreading unless it is absolutely necessary for a script to have no script delay. A multithreaded script can cause problems for dozens or even hundreds of users if done needlessly. Multithreading can cause unnecessary strain on sims or other servers, and should be avoided when possible.

An example of multithreading would be having a script that would need to send an email every time something occured. By using llMessageLinked, another script in the same object could be told to send the email instead, thereby sparing the main script from llEmail's built-in 20-second delay.

Example added by MarcEisenberg:
This is the mailing system script. Only one of these scripts in the linked object hierarchy is necessary:
integer LINKMSG_SEND_EMAIL = 1451363;

default {
    link_message(integer sender_num, integer num, string str, key id) {
        if (num == LINKMSG_SEND_EMAIL) {
            list mailparam_list = llCSV2List(str);
            string mailto_str = llBase64ToString(llList2String(mailparam_list, 0));
            string mailsubj_str = llBase64ToString(llList2String(mailparam_list, 1));
            string mailbody_str = llBase64ToString(llList2String(mailparam_list, 2));
            llEmail(mailto_str, mailsubj_str, mailbody_str);
        }
    }
}

This is the script that tells the mailing system it needs to email. Include this at the top of any script that will send mail in a linked object hierarchy:
integer LINKMSG_SEND_EMAIL = 1451363;

MT_SendEmail(string mailto_str, string mailsubj_str, string mailbody_str)
{
    llMessageLinked(LINK_SET, LINKMSG_SEND_EMAIL,
                    llList2CSV([llStringToBase64(mailto_str),
                                llStringToBase64(mailsubj_str),
                                llStringToBase64(mailbody_str)]),
                    NULL_KEY);
}

Then, messages are sent using this: MT_SendEmail("email@domain.com", "Subject", "This is my message");

The message will be passed off to the mail-handling script.

IMPORTANT! Please note that this example uses the number field in the linked messages as an identifier for the type of message. If there are scripts in a linked object that send messages in another way, this script's link handling will need to be modified to match (or there may be problems with miscommunications between objects).


Communications
There are 5 comments on this page. [Display comments/form]