Squirrel

The programming language
Welcome to Squirrel Sign in | Join | Help
in Search

Get current thread object

Last post 07-14-2009, 1:06 AM by fagiano. 3 replies.
Sort Posts: Previous Next
  •  07-01-2009, 3:28 PM 3342

    Get current thread object

    Is there a way that I can retrieve the thread object that is currently executing? For example:

    function thread1() {
        myThread = ::currentthread(); // Would give me "a" from below
    }

    a = ::newthread(thread1);
    a.call();


    I think I know of a way to do this in native code, but it would be nice if Squirrel supported something like this within the scripts.
  •  07-03-2009, 12:40 AM 3347 in reply to 3342

    Re: Get current thread object

    no at the moment is not possible, however would be quite simple to add. I find this a bit dangerous because it would create a reference cycle, as the thread reference itself. If you do something of the kind you should always be careful tu use a weak reference to the thread.

    Alberto
  •  07-13-2009, 8:18 PM 3359 in reply to 3347

    Re: Get current thread object

    So I finally had a chance to get back to this idea. It took a bit of poking around in the Squirrel code to figure out how to do this nicely, but it appears that this works:

    SQInteger sq_getcurrentthread(HSQUIRRELVM v) {
        HSQOBJECT threadObj;
        threadObj._type = OT_THREAD;
        threadObj._unVal.pThread = v;
       
        sq_pushobject(v, threadObj);
        sq_weakref(v, -1);
        return 1;
    }


    The end result being a weak reference to the current thread object. Does this seem like a safe way of achieving this functionality? The bit about manually creating the object to push worries me, but it's working in my test scenario.
  •  07-14-2009, 1:06 AM 3360 in reply to 3359

    Re: Get current thread object

    Yep, that should work just fine.

    Alberto

View as RSS news feed in XML
Powered by Community Server, by Telligent Systems