Squirrel
The programming language
Welcome to Squirrel
Sign in
|
Join
|
Help
in
The Language (forum)
General (group)
(Entire Site)
Search
Home
Forums
Squirrel
»
General
»
The Language
»
Get current thread object
Get current thread object
Last post 07-14-2009, 1:06 AM by
fagiano
. 3 replies.
Sort Posts:
Oldest to newest
Newest to oldest
Previous
Next
07-01-2009, 3:28 PM
3342
Toji
Joined on 08-21-2008
Utah, United States
Posts 99
Get current thread object
Reply
Quote
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.
Report abuse
07-03-2009, 12:40 AM
3347
in reply to
3342
fagiano
Joined on 06-12-2005
Posts 535
Re: Get current thread object
Reply
Quote
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
Report abuse
07-13-2009, 8:18 PM
3359
in reply to
3347
Toji
Joined on 08-21-2008
Utah, United States
Posts 99
Re: Get current thread object
Reply
Quote
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.
Report abuse
07-14-2009, 1:06 AM
3360
in reply to
3359
fagiano
Joined on 06-12-2005
Posts 535
Re: Get current thread object
Reply
Quote
Yep, that should work just fine.
Alberto
Report abuse
Alberto Demichelis 2003-2008