Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > non-blocking mutual exclusion

Reply
Thread Tools

non-blocking mutual exclusion

 
 
Giambattista Bloisi
Guest
Posts: n/a
 
      12-16-2003
Hi,
In your opinion there is a faster way to implement a non-blocking
multual exclusion than:

Object sem = new Object();
int count = 0;

void mutex_activity() {
synchronized(sem) {
if(count > 0)
return;
count++;
}

//... activity

synchronized(sem) {
count--;
}
}

I tryed with the following, but it seems to not work properly.

volatile int count = 0;

void mutex_activity() {
if(++count > 1) {
--count;
return;
}

//activity...

--count;
}

Regards,
Giambattista


 
Reply With Quote
 
 
 
 
Matt Humphrey
Guest
Posts: n/a
 
      12-16-2003

"Giambattista Bloisi" <(E-Mail Removed)> wrote in message
news:brmt9p$l2a$(E-Mail Removed)...
> Hi,
> In your opinion there is a faster way to implement a non-blocking
> multual exclusion than:


The performance of locking really depends on how it is implemented in the
underlying JVM. If your locking is correct (a big task to begin with) try
profiling your code to see if that's where the problem is. Otherwise you'll
be optimizing the wrong thing.

<code snip>

> I tryed with the following, but it seems to not work properly.
>
> volatile int count = 0;
>
> void mutex_activity() {
> if(++count > 1) {
> --count;
> return;
> }
>
> //activity...
>
> --count;
> }
>
> Regards,
> Giambattista
>


From p. 97 of Doug Lea's book "In particular, composite read/write
operations such as the "++" operation on volatile variables are not
performed atomically." Try looking up java.util.concurrent.atomic.

Cheers,


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
for mutual exclusion of n processes - algorithm. ppp Java 1 12-05-2006 01:50 PM
mutual exclusion of n processes - algorithm. ppp Java 0 12-05-2006 01:35 PM
mutual exclusion of n processes - algorithm. ppp Java 0 12-05-2006 01:35 PM
SGI: Need Something like 1 Mutual Exclusion Semaphore for Multible Programs clusardi2k@aol.com C Programming 4 07-07-2005 09:26 PM



Advertisments