Error when multithreading

Feb 9, 2011 at 10:35 PM

I am attempting to do a simple example of of JINT in a mulitthreaded environment.

 

Basically I took the program.cs of the sample temp program (Jint.temp) which runs a number of performance scripts and place them in a class.  I create several instances of the class and start them with Thread.start.  I then get an error...

An item with the same key has already been added.

 

#1 - Can Jint be multithreaded?  I would think that since each is using it's own engine it would be possible.

#2 - What am I adding of the same name?

 

Any help would be appreciated.

Feb 10, 2011 at 9:01 PM

Currently jint doesn't support multithreading and attempts to do so can lead to unpredictable results.

There are two problems while running multiple threads:

1. Concurent access to an execution context which is represented by an instance of ExecutionVisitor.

2. Concurent access to variable objects.

Fist problem can be solved by creating a new ExecutionVisitor instance for every thead.

Second problem a little more complex since if we add a syncronization code it will lead to an overhead in a single threaded environment (1.5-2 times slower).

Feb 10, 2011 at 9:40 PM

Well, I thought I was overcoming #1 in that each thread instantiated a new engine.

JintEngine engine = new JintEngine();

engine.DisableSecurity();

engine.Run("1;");

ExecutionVisitor visitor = engine.visitor;

Where would I look to consider #2 with Synchronization?

Jeff

From: cin [email removed]
Sent: Thursday, February 10, 2011 4:02 PM
To: jeff@therosenthalwebsite.com
Subject: Re: Error when multithreading [jint:245357]

From: cin

Currently jint doesn't support multithreading and attempts to do so can lead to unpredictable results.

There are two problems while running multiple threads:

1. Concurent access to an execution context which is represented by an instance of ExecutionVisitor.

2. Concurent access to variable objects.

Fist problem can be solved by creating a new ExecutionVisitor instance for every thead.

Second problem a little more complex since if we add a syncronization code it will lead to an overhead in a single threaded environment (1.5-2 times slower).

May 16, 2012 at 10:58 AM

I apologize for resurrecting an old thread ...

 

Is there any solution to this yet  :)

 

I am evaluating this for use in one of our applications but I will need to be able to have multiple instances of the engine running different script.

Concurrent Access to objects will not be required as every instance will be running in ClrEnable = false mode and only interacting through explicitly set delegates.

 

The thread discusses creating different ExecutionVisitors but there is no property on the JinyEngine object to allow this.

Is this only a hypothetical solution that is not yet implemented or something that is  to come in the next version?

May 17, 2012 at 5:23 PM
Edited May 17, 2012 at 7:45 PM

mutithreading will break debug capabilities such as call stack, since there will be multiple execution visitors, it will also hit a performance in  the access synchronization to the global (at least) object.

There could be an easy alternative to apply a SyncronizaedAttribute to the execution visitor. When I played in this way it solves multithreading problems, but also hits a performance approximately 3 times.