Closed Thread Icon

Topic awaiting preservation: mutex in my java script (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8940" title="Pages that link to Topic awaiting preservation: mutex in my java script (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: mutex in my java script <span class="small">(Page 1 of 1)</span>\

 
Yossi Admon
Bipolar (III) Inmate

From: Israel
Insane since: Nov 2001

posted posted 11-10-2003 13:58

Do you have any idea, how can I implement a mutex in my java script code?


10x in advanced
Yossi Admon

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 11-10-2003 14:01

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

Hugh
Paranoid (IV) Inmate

From: Dublin, Ireland
Insane since: Jul 2000

posted posted 11-10-2003 14:30

Mutex:
(1) Short for mutual exclusion object. In computer programming, a mutex is a program object that allows multiple program threads to share the same resource, such as file access, but not simultaneously. When a program is started, a mutex is created with a unique name. After this stage, any thread that needs the resource must lock the mutex from other threads while it is using the resource. The mutex is set to unlock when the data is no longer needed or the routine is finished.

First time I ever heard of a mutex. but no. javascript has no control over such things. What is it your trying to achieve ?

"You cannot be told what the mutex is.... "




Yossi Admon
Bipolar (III) Inmate

From: Israel
Insane since: Nov 2001

posted posted 11-10-2003 14:45

My page loads data from the server and according to the user events and I want to avoid 2 parallel calls.

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 11-10-2003 14:53

The only areas I've heard of mutex was for kernel programming.
Yossi Admon: Do you want to share a ressource of Javascript with the OS and vice-versa of do you want to make a sort of multi threading handler with mutex in JavaScript ?

In the first case, I fear that's impossible unless some evil ActiveX allows that but I wouldn't recommend that solution.
Else you should find some information on how to implement your own mutex process in the comp.programming.threads newsgroups and by searching for mutex there.

[edit]
Yossi Admon:

quote:
My page loads data from the server and according to the user events and I want to avoid 2 parallel calls.

Ok, so you should be able to do the mutex by hand. Simply put a sort of flag ( e.g. a file, a record in a database ... ) that tells that a process already works on the datas. The process must wait until the flag is off ( e.g. the file no longer exist, the record is set to false ... ) to start. Once the process is over, simply unset the flag to allow the concurent process to start.
[/edit]

Hope that helps.

Mathieu "POÏ" HENRI

[This message has been edited by poi (edited 11-10-2003).]

Yossi Admon
Bipolar (III) Inmate

From: Israel
Insane since: Nov 2001

posted posted 11-10-2003 17:23

Do we have In JavaScript a way I can stop/wait/sleep with no busy loop?
If there is, the following code will implement a mutex (99.99% safe).
I think the work with mutex is for offline events (like setInterval/setTimeout) and online events (click/over...)

var mutex = 0;
function EnterCriticalSection(mutex){
while(mutex) Sleep(100);
mutex = 1;
}

function LeaveCriticalSection(mutex){
mutex = 0;
}

......
function SafeLoadData(...){
// Only one thread at a time may execute the code between the
// calls to enter and leave. Here a shared resource may be
// safely manipulated.
EnterCriticalSection(mutex);

...secure code...

LeaveCriticalSection(&cs);
}



Hugh
Paranoid (IV) Inmate

From: Dublin, Ireland
Insane since: Jul 2000

posted posted 11-12-2003 11:37

You can make a sleep command this way:

var thread;

function start() {
thread = setInterval("run()",200);
}

function stop() {
clearTimeout(thread);
}

function sleep(time) {
stop();
setTimeout("start()",time);
}

function run() {
// whatever runs a lot of times.
}

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 11-12-2003 18:06

A language which does not have threads (such as JavaScript) does not need mutexes. If you want to avoid doing certain things while you're waiting for something else to complete, you can simply keep a flag variable which says that you're waiting, and if it's true, you don't do anything else (if (waiting) return . Then, when you're done doing whatever you're waiting for, you go back and do the things that you wanted to do while you were waiting.

Yossi Admon
Bipolar (III) Inmate

From: Israel
Insane since: Nov 2001

posted posted 11-12-2003 18:20

It looks like the load of outside XML open a new connection to the server and works asynchronic.
If this action was synchronic, there was no problem.

Yossi Admon

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 11-12-2003 21:51

Despite the XML loading being asynchronic, there is only one "thread" (your javascript program) which needs to access the "critical section" (the launching and recieving of an HTTP request). Two JavaScript functions cannot be run simultaneously, so I think you're overcomplicating things.

Yossi Admon
Bipolar (III) Inmate

From: Israel
Insane since: Nov 2001

posted posted 11-13-2003 07:51

My problem is loading XML files in a safe mode.
What about events coming from other windows is it still thread safe?
It looks like the number of threads allow to be use in a browser window is 2 (IE default) and it is defined as a registry key.


Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 11-13-2003 08:02

I believe you can be guaranteed that two javascript functions in the same window will never be executed at the same time. If you have multiple windows, simply communicate between them and make sure only one of them actually does the stuff that can't be done more than once at a time.

« BackwardsOnwards »

Show Forum Drop Down Menu