Which scripting language is better for embedding in multi-threaded C/C++ application [closed]
Considering the following requiremen开发者_开发技巧tes:
- Must be supported on Windows. Preferably works also on other platforms.
- Must support multi threading. By that I mean that the engine can work in parallel in multiple threads.
- Readability is important.
- License must be compatible with closed-source projects.
I like Python for its readablity. I am also have more experience with Python than other scripting languages. However CPython is not multi-threaded, and IronPython requires hosting the CLR and a compatible language (C++/CLI or C#).
Lua might be worth checking out. It can be used in a thread-safe manner, and the language supports a 'co-routine' concept which might fit your requirements.
Lua is the best choice. Python, Ruby and JavaScript are big languages and they are not designed for to embed. But Lua is different, designed to embed.
You should consider the "restriction" more than any other things for your script language. Embed scripts can use for hack (bad meaning) easily.
For example, by default Lua can not print to console. As I know, Blizzard uses the Lua because of that.
I've been in the same dilemma an choose Lua over Python and JScript. The thing which Lua does best is the great interop with C/C++ code using libraries like luabridge and luabind. That is, you can call lua from C++ and have the script call back into C++ without a problem, access c++ data from the script and vice versa.
The problem with languages like Python and Lua is that the language is not really multi-threaded in the regular sense of the word: if one C++ thread it using the language scripting engine to run a script, you cannot use the same engine to run another script. Both languages has an engine-wide lock which can be used in those cases to make sure the engine integrity is maintained. However, both languages are multi-threaded in the sense that you can run function in the background and interact with any synchronization object you want (just like from C++). So I choose to have all threads created from C++ and scripting code only run in a dedicated threads (thread per engine) and interact with other threads in the application in the regular ways.
If you need to pass data and control from C++ to a script and vice versa, Lua is much better than Python. Beside that, I would not host the CLR in a C++ project. It's too messy.
You might consider embedding a popular JavaScript engine. Not only will they be fast and well-supported, but so many people know how to program in JavaScript that it will be easily adopted and read by a large audience.
According to this answer the SpiderMonkey engine is thread-safe, while Google/Chrome's V8 may not be.
JScript is a great solution. It is already supported with Windows Script Host and emulates multithreading with events. It is easier to use than Python, I promise you. This MSDN article is a great reference.
One could use Guile, which is embeddable Scheme.
Lua's really easy to integrate, and can work in multiple threads using something like Lua Lanes (which is cross platform Windows/ Linux/ MacOS).
Im pretty sure Stackless Python is going to be the only one supporting multithreading out the box. Stackless Python was chosen by CCP for their MMO: Eve-Online specifically because the stackless nature of the code allowed them to schedule the continuations on any OS thread they needed once they'd built the necessary primitives in to make the whole thing thread safe.
Lua can be used in a multi threaded environment, but each concurrent thread would need a separate lua_State object so you'd need to build your own interthread message passing system for lua code, executing in the context of separate lua states, to communicate.
You can try spidermonkey. Check out the Libjspp C++ template based wrapper for embedding and extending Javascript engine spidermonkey: http://code.google.com/p/libjspp/
Entirely preference-based. Most languages have a way to embed in C with options for exports into the scripting environment.
If it were me, I'd go with V8 Javascript.
By "multithreaded" I'm assuming you mean "can effectively exploit multiple cores"? If your system only has a single CPU, then you're only going to be done one thing at a time regardless of the scripting language you choose.
If you do decide to go the CPython route, then there the main thing to remember is that it is only the scripting engine itself that is protected by the global interpreter lock. If the script being executed spends a lot of time calling out to non-Python code (including I/O and other system level operations) then it can exploit multiple threads quite happily.
Another factor to consider is that Python's introspection capabilities make it inherently difficult to secure properly (Google have done it for AppEngine, but they had to disallow quite a few things in the process).
Given the prevalence of Javascript engines in browsers and Lua engines in PC games, one of those is likely to be an easier way forward.
Try Falcon programming language
精彩评论