I’m a swiss Master student in Computer Science. I’m very interested in C++, open source projects, Linux, Intel Assembly and Agile. I'm currently working on Eddi, a new programming language that I created to improve my skills in C++. I've also worked a lot on Java technologies (Sprint, Osgi, Play!, ...), but I'm not currently working with Java anymore. Baptiste is a DZone MVB and is not an employee of DZone and has posted 51 posts at DZone. You can read more from them at their website. View Full User Profile

Introduction to the JR Programming Language

06.29.2010
| 7076 views |
  • submit to reddit

Semaphores

We will now see how to use one of the basic concept of concurrent programming : the semaphores. The semaphores are a really simple concept but very powerful. A semaphore represent a certain integer valur representing the number of threads that can go in a certain portion of code, call it “s”. A semaphore has two actions :

  • P : make the thread wait while s equals 0 and then decrement s.
  • V : increment s.

This two operations are atomic. We use the semaphores to protect a critical section who need to be executed in an atomic way. We can also use semaphores to restrain the number of threads that can execute some instructions in parralel.

The declaration and the use of semaphores is really easy. Here is how you can declare a semaphore with a initial value of 1 :

sem mutex  = 1;

Then, the operations P et V are extremely simple to use :

P(mutex);
//Critical section
V(mutex);

The semaphores are principally used to solve the critical section problem. Imagine a simple example, but who show exactly the problem that the semaphore can solve :

private static int value = 0;

static process Calculator((int id = 0; id < 50; id++)){
for(int i = 0; i < 5; i++){
value = value + 2;
}
}

Because this code launch 50 threads who add each one 5 times 2 to value, we could think that value must be 500 at the end of the execution, isnt’it ?

But nothing guarantee that result. This is known as the interleaving in concurrent programming. This is because the + 2 operation is in reality 3 operations :

  • read the value of value
  • add 2 to the read value
  • set the new value to value

A thread can be put in wait just after 1 and do the incrementation on the old value but other threads have still made the incrementation, but it has the old value when it mades the +2 and write and false value to value. To prove that to you, execute the following code several times :

import edu.ucdavis.jr.JR;

public class SemaphoreProcess {
private static int value = 0;

static process Calculator((int id = 0; id < 50; id++)){
for(int i = 0; i < 5; i++){
value = value + 2;
}
}

public static void main(String[] args){
try {
JR.registerQuiescenceAction(end);
} catch (edu.ucdavis.jr.QuiescenceRegistrationException e){
e.printStackTrace();
}
}

public static op void end(){
System.out.println(value);
}
}

On my computer, i’ve the following results.

498
500
500
500
496

And if we use greater values, it’s even worse. By example, with 100 threads and 100 iterations :

20000
19560
19912
19758
20000

But this problem can be solved with semaphores :

private static sem mutex = 1;
private static int value = 0;

static process Calculator((int id = 0; id < 50; id++)){
for(int i = 0; i < 5; i++){
P(mutex);
value = value + 2;
V(mutex);
}
}

With that, we have the guarantee that only one thread can do the incrementation at a time and make it atomic. Then all the executions will finish with a value of 500. But that of course impact the performances because instead of x threads who made operations in a parralel way, we’ve now only one thread at a time. The example with 100 threads and 100 iterations is really slow. We can improve performance using the mutex semaphore around the loop. But the performances are to be considered differently in each example. So we must use the synchronization methods of threads wisely.

Conclusion

So, we’ve now discover the main concepts of the JR programming language. Like you have see in this article, this programming language makes concurrent programming concepts easier.

I hope that this article has been useful to you to discover the JR Programming Language, and why not learn and use this language.

From http://www.baptiste-wicht.com/2010/01/jr-introduction/

Published at DZone with permission of Baptiste Wicht, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)