Thread safety in Actionscript

Post here your questions about Actionscript and Java server side extensions development.

Moderators: Lapo, Bax

Post Reply
dolly
Posts: 10
Joined: 18 Oct 2007, 05:30

Thread safety in Actionscript

Post by dolly »

Hi~

Although it is mentioned in the document that actionscript provides thread safety internally.
I am still confused.
I list 2 questions in the comments below.

Code: Select all


// global variable
var lock = false;

function handleRequest(cmd, params, user, fromRoom)
{
	if (!lock) {

		// Question1: Is it possible that mutiple threads  enter this block concurrently?

		lock = true;

		// Question2: Is the code here thread safe?

		lock =false;

	}
}
Please help me answer the questions.
Thanks.
BigFIsh
Posts: 1698
Joined: 25 Feb 2008, 19:26
Location: New Zealand

Post by BigFIsh »

As far as I recall, Actionscript does not support thread safety (yet). See http://blogs.adobe.com/aharui/2008/01/t ... ipt_3.html

Where in the docs did it say that it does?
Smartfox's forum is my daily newspaper.
dolly
Posts: 10
Joined: 18 Oct 2007, 05:30

Post by dolly »

Hi~

You're right.
Actionscript does not support thread safety itself.
What I am confused is in this document "Developing Thread Safe Extensions".

It says
Thread safety is already encapsulated in these language implementations so you should be able to use the Actionscript or Python objects and native collections without worries.
Does it mean the code inside the if block above will be properly implemented as thread safe?

Thanks.
BigFIsh
Posts: 1698
Joined: 25 Feb 2008, 19:26
Location: New Zealand

Post by BigFIsh »

If you read the first sentence of that paragraph, it indicates that all languages will be compiled into java at time time on server side. So, your actionscript extension will be compiled into a generated java extension.
Smartfox's forum is my daily newspaper.
dolly
Posts: 10
Joined: 18 Oct 2007, 05:30

Post by dolly »

Hi~

That is exactly what I am confused now.
I am wondering that if the code below will be thread safety after being compiled into a generated java extension.

All I want is to make sure a specific action will not being executed by mutiple threads concurrently.
So I declare a global variable "lock" to prevent it.
But I am not sure if this will work.
There is one question list inside the code using comment.
That is what I am concerned.
Please help me answer the question.

Thanks.

Code: Select all


// global variable 
var lock = false; 

function handleRequest(cmd, params, user, fromRoom) 
{ 
   if (!lock) { 

      lock = true; 

      // Specific action - for example, unlock a treasure box
      // Question: 
      // Is it possible that mutiple threads  execute the code here concurrently? 

      lock =false; 

   } 
} 

BigFIsh
Posts: 1698
Joined: 25 Feb 2008, 19:26
Location: New Zealand

Post by BigFIsh »

Oh I see now where you're coming from.

I'm no expert in thread safety, but I'll try and answer.

The server has three main threads by default: SysHandlerThreads, ExtHandlerThreads and OutQueueThreads. The number of threads for these are 1 by default, but can be increased via config.xml file.

According to the doc, handleRequest is handled via SysHandlerThreads. So, if you had one system handler thread, then there's nothing to worry about as all requests will be lined up in one waiting line. However, if you had two system handler threads, then it may be possible that the thread will be executed concurrently (but not often).


As for your code - I'm not really sure if that would work. It is possible that two clients sets the lock = true at the same time (only if you had multiple threads)


I'm not sure how to create your own thread in actionscript, but it can be done in java. Maybe you could do something like this in actionscript:

1. Gets the request from client
2. Push it into an array (a waiting line)
3. Use Scheduler (sort of like Timer/SetInterval) to get the first request in the array (shift) and then execute your code. This way, your code won't be executed concurrently.

Never tried this, could be messy..
Smartfox's forum is my daily newspaper.
dolly
Posts: 10
Joined: 18 Oct 2007, 05:30

Post by dolly »

Many thanks.
It's a great help for me. :)
Post Reply