YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

Multy-threads (thread-safe)
strijar
#1 Posted : Monday, September 27, 2010 12:00:36 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 9/27/2010(UTC)
Posts: 2
Location: Russia, SPb

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
Hello!

Possible to do this?
fagiano
#2 Posted : Monday, September 27, 2010 12:43:08 PM(UTC)
Rank: Advanced Member

Groups: Registered, Administrators
Joined: 6/11/2005(UTC)
Posts: 1,056

Thanks: 0 times
Was thanked: 78 time(s) in 60 post(s)

No is not possible , Squirrel is not thread safe and it will never be. The main reason is:
It is too inefficient to write an interpreter for a dynamic language that is thread-safe, you'd have to acquire a global lock for almost every instruction. Phyton has some option to do multithreading VMs, but is so slow that nobody uses it.
Even a thread-safe Api doesn't make much sense, just wrapping all api in a mutex will still require you to lock calls in bunches. Basically, Squirrel doesn't do multithreading all calls to a specific VM should exists in the same thread.

ciao
Alberto
Follow me on Twitter @squirrellang
strijar
#3 Posted : Monday, September 27, 2010 1:32:58 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 9/27/2010(UTC)
Posts: 2
Location: Russia, SPb

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
Lua have a patch allowing to use threads (I use it). And it reduces the performance only 30%.
atai
#4 Posted : Monday, September 27, 2010 4:57:27 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 8/16/2005(UTC)
Posts: 310

Thanks: 18 times
Was thanked: 18 time(s) in 14 post(s)
There is a difference between Squirrel and Python in this regard, unless I am mistaken...

Squirrel allows multiple interpreters in the same process, each interpreter being safe from another if each is used in a single thread. Python only allows one interpreter per process and a global lock is needed to be obtained if more than one thread needs to access Python services. Is this right?


fagiano wrote:

No is not possible , Squirrel is not thread safe and it will never be. The main reason is:
It is too inefficient to write an interpreter for a dynamic language that is thread-safe, you'd have to acquire a global lock for almost every instruction. Phyton has some option to do multithreading VMs, but is so slow that nobody uses it.
Even a thread-safe Api doesn't make much sense, just wrapping all api in a mutex will still require you to lock calls in bunches. Basically, Squirrel doesn't do multithreading all calls to a specific VM should exists in the same thread.

ciao
Alberto
marius
#5 Posted : Monday, October 18, 2010 3:51:47 AM(UTC)
Rank: Member

Groups: Registered
Joined: 10/18/2010(UTC)
Posts: 14
Location: Canada

Thanks: 0 times
Was thanked: 1 time(s) in 1 post(s)
Not multi-threaded but you can have an sq VM in
different threads as: I run up to 400 threads
using a thread local storage of the VM as.

Make following changes in squirel source code:

Windows:
Code:
__declspec(thread) static HSQUIRRELVM _VM;
__declspec(thread) static int _CallState;
__declspec(thread) static SquirrelObject * _root
;

Linux:
Code:
thread static HSQUIRRELVM _VM;
thread static int _CallState;
thread static SquirrelObject * _root;




Remarks: each thread should call something like:
Code:
SquirrelVM::Init();
sq_setprintfunc(SquirrelVM::GetVMPtr(), printfunc); //sets the print function
_oscript = SquirrelVM::CompileScript(_script.c_str());
SquirrelVM::RunScript(_oscript);
SquirrelVM::Shutdown();
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Clean Slate theme by Jaben Cargman (Tiny Gecko)
Powered by YAF 1.9.4 | YAF © 2003-2010, Yet Another Forum.NET
This page was generated in 0.173 seconds.