• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
justin12343

Multithreaded script execution

2 posts in this topic

I have been using AngelScript in my engine for a while and now I'm trying out its multithreading capabilities. There's not much documentation on this subject, so I'm struggling a bit. I'm trying to acheive a variable amount of threads, maybe 2 or 3, to be used to execute multiple scripts at a given time. The documentation says more than one engine can be used right? Can I only use one? Has anybody had any luck with multithreading with angelscript and does anyone have any sample code available?

 

I'm trying to keep it as simple as possible:

 

This is my execution function that my threads call:

int threadExecuteContext( void *data)
{
	asIScriptContext *ctx = (asIScriptContext*)data;

	int r = ctx->Execute();

	if (r != asEXECUTION_FINISHED)
	{
		if (r == asEXECUTION_EXCEPTION)
			std::cout << "An exception occurred while executing function on thread id " << SDL_ThreadID() << "." << std::endl;

		if (r == asEXECUTION_ERROR)
			std::cout << "An error occurred while executing function on thread id " << SDL_ThreadID() << "." << std::endl;

		return asERROR;
	}
	else
	{
		ctx->Unprepare();
	}

	return 0;	
}

This one prepares a function to be called and waits for a available thread:

int ScriptManager::ExecuteFunction( asIScriptFunction *func, asIScriptObject *obj)
{
	asIScriptContext *ctx = GetContext();

	if (ctx == NULL)
		return asERROR;

	if (ctx->Prepare( func) < 0)
	{
		std::cout << "Could not prepare script function" << std::endl;
		return asERROR;
	}

	ctx->SetObject( obj);

	SDL_Thread *th = NULL;

	while (th == NULL)
		th = GetThread( ctx);

	return asSUCCESS;
}

And lastly here is my GetThread function. It maintains the maximum thread limit by waiting for one to finish:

SDL_Thread *ScriptManager::GetThread( asIScriptContext *ctx)
{
	SDL_Thread *th = NULL;

	if (threads.size() < MAX_THREADS)
	{
		th = SDL_CreateThread( threadExecuteContext, "Script Execution Thread", ctx);
		threads.push_back( th);
	}
	else
	{
		int returned;
		thread_iterator it = threads.begin();

		SDL_WaitThread( *it, &returned);
		threads.erase( it);

		th = SDL_CreateThread( threadExecuteContext, "Script Execution Thread", ctx);
		threads.push_back( th);
	}

	return th;
}

This method has problems. The context always has an unintialized state and does not execute anything.

Edited by justin12343
0

Share this post


Link to post
Share on other sites

You can use a single engine with multiple threads. There is not much documentation, because there really isn't much to say about it. 

 

You say the asIScriptContext is always uninitialized when the thread starts. This leads me to believe that you're reusing the same asIScriptContext for all executions. This will obviously cause problems, because the context is the internal state of an execution. It holds the call stack, program position, etc.

 

How does your method GetContext() look like? 

 

 

Finally, a general warning about multithreading. While AngelScript's internal objects are threadsafe and shouldn't cause any problems, you will need to make sure the scripts executed in parallel are properly synchronized while accessing the same data, e.g. application registered resources, or global variables in the modules, etc. The easiest way of guaranteeing proper functioning is by making sure the scripts don't have access to the same data at all, but it may not be possible for everything in which case you'll need to add the synchronization mechanisms.

0

Share this post


Link to post
Share on other sites
asIScriptContext *ScriptManager::GetContext()
{
	asIScriptContext *ctx = NULL;
	std::vector<asIScriptContext*>::iterator it;
	
	if (!contexts.empty())
	{
		for (it = contexts.begin(); it != contexts.end(); ++it)
		{
			ctx = *it;

			if (ctx->GetState() != asEXECUTION_ACTIVE)
			{
				return ctx;
			}
		}
	}

	ctx = engine->CreateContext();

#if defined(_DEBUG) || defined(_DEBUG_RELEASE)
	peek->AddContext( ctx);
#endif

	contexts.push_back( ctx);

	return ctx;
}

I can see how this will cause problems with multiple threads attempting to access a context since it tries to reuse a existing one if it can. Maybe I should read more about threading and try to come up with a more efficient model because right now I'm just constantly creating threads when I could just create the number of execution threads needed and some how feed contexts to them.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0