Welcome Guest Search | Active Topics | Log In | Register

Problem with calling functions from another thread.
#1 Posted : Friday, March 17, 2017 7:47:42 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 3/17/2017(UTC)
Posts: 1
Location: Poland

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
Hello there.
I've been trying to make separate thread and then use functions from parent thread but every time i call exported function from another thread it will immediately crash my app,
althought i can call core functions such as print() from another thread withouth problem.
It's my second day with this language so I'm not really familiar with the api yet but would like you to take a look at my code and eventually tell me what's wrong with it.

This is how I create a thread:
vm = sq_newthread(friendVm, 1024);

if (!vm) {
    throw squall::squirrel_error("Couldn't create new thread.");


if (SQ_FAILED(sq_getstackobj(friendVm, -1, &handle))) {
    throw squall::squirrel_error("Couldn't get squirrel thread from stack.");

sq_addref(friendVm, &handle);
sq_pop(friendVm, 1);

and this is how i run it:


if (SQ_FAILED(sq_compilebuffer(vm, code.c_str(),
    code.length(), "", SQTrue)))
    throw squall::squirrel_error("Unable to compile a script");;


if (SQ_FAILED(sq_call(vm, 1, SQFalse, SQTrue))) {
    throw squall::squirrel_error("Unable to run script");

I'm almost sure that i missed something but to be honest.. I do not know what!

#2 Posted : Sunday, March 19, 2017 2:47:41 AM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 8/23/2014(UTC)
Posts: 103
Location: Northern Germany & Lincolnshire, U.K.

Thanks: 1 times
Was thanked: 8 time(s) in 8 post(s)
do you use REAL threads? (pthread for example or the like?)
Squirrel is NOT threadsafe.
A Squirrel thread is also called "friend VM". It's another context (basically just another stack) for the SAME VM! All objects are shared.
(It's mainly intended for generators and has nothing to do with CPU multithreading support)

So if you use squirrel VMs in more than one CPU thread - then you MUST keep them and all of their objects strictly separated (which, as friend VMs share the objects, is not the case with them!)

Users browsing this topic
Guest (3)
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.062 seconds.