cancel
Showing results for 
Search instead for 
Did you mean: 

thread safe Increment of counter variables

asteinberg
Discoverer
0 Kudos

Hello guys,

do anyone of you know a thread safe way to increment variables (In this case global variables in IdM 7.2.)
in global/public javascripts.

If I'm using uGet...Var and uSet...Var I expierience possible race conditions, as I have no semaphore embracing both calls.

Thanks in reward,

Ansgar

former_member2987
Active Contributor

Hi Ansgar,

Not sure what your problem is here. I've not seen any issues when incrementing Global Vars, and I've done it in some fairly high load situations.

Can you explain some more as to what's going on and include what version of IDM you are using?

Thanks!

Accepted Solutions (1)

Accepted Solutions (1)

asteinberg
Discoverer
0 Kudos

Hi Matt,

thanks for your ideas. I decided to use a database sequence instead. This is not that transparent to the developers but it is easy and failsafe.

former_member2987
Active Contributor
0 Kudos

Probably so.

Please mark your enty as the answer so the thread gets closed off.

Matt

Answers (1)

Answers (1)

asteinberg
Discoverer
0 Kudos

Hi Matt,

I'm using a global counter in a global javascript as follows

// fetch the global var<br>var numStr = uGetGlobalVar("A_GLOBAL_VAR");
//increment the value (only symbolic code as we need some string conversion)
numStr++;
do something with the number
// set the global var
uSetGlobalVar("A_GLOBAL_VAR", numStr );

This script is called from different places, so that it happens that the script is running 2 times at the same time.

If the second caller fetches A_GLOBAL_VAR before the first caller has called A_GLOBAL_VAR both callers are using the same value and the global var is only incremented once.

Hope that makes it more clear.

former_member2987
Active Contributor
0 Kudos

Yes, I suppose it does. That could be a tough one to crack.but I could think of a couple of approaches, all theoretical, but it might give you something to work with.

1. After each task which updates the variable put a pause/sleep in to allow other processes to stop. This probably won't scale.

2. Can you use an attribute or create some sort of flag that can be used by the process to see if it is "checked out" and wait for it to clear before going on? The value of the flag will probably need to be a date/timestamp.

Not really sure how you would set these up, unfortunately I do not have the bandwidth to prototype something like this at the moment, but maybe it will spark an idea for you or someone else in the community.