views:

648

answers:

9

found in torvalds/linux-2.6.git -> kernel/mutex.c line 171

i have tried to find it on google and such to no avail.

what does "for (;;)" instruct?

+32  A: 

It literally means "do nothing, until nothing happens and at each step, do nothing to prepare for the next". Basically, it's an infinite loop that you'll have to break somehow from within using a break, return or goto statement.

Blindy
Thumbs up for the speed :)
Anna
+4  A: 

It loops forever (until the code inside the loop calls break or return, of course. while(1) is equivalent, I personally find it more logical to use that.

Wim
+3  A: 

It's equivalent to while( true )

Edit: Since there's been some debate sparked by my answer (good debate, mind you) it should be clarified that this is not entirely accurate for C programs not written to C99 and beyond wherein stdbool.h has set the value of true = 1.

wheaties
provided that true == 1.
GregS
They need a comment downvoting system.
Blindy
Hey man, he's just trying to make sure people future-proof their applications against the possibility of the value of 'true' changing.
Frank Farmer
Ugh, the pain of a language where "1" is a more authoritative "True" value than a boolean "true".
Clueless
@Blindy: Why would you downvote his comment? @Frank: Changing from what? true is not defined in standard C.
sepp2k
In most languages true means !0
Ton
@GregS: No. Provided that true != 0 :)
AndreyT
@sepp2k: `true` is defined in `stdbool.h` of C99
AndreyT
Ok, fair enough, but I doubt the linux kernel uses C99.
sepp2k
Is that really worth to argue? And i cannot see any reason for downvoting this answer (i see this answer is downvoted). GregS' comment may be counted as a contribution to answer. And we provided the info that **true** is not defined in some versions of C.
JCasso
@Ton: In most langages, true doesn't mean !0. Most of them have true/false concepts that aren't just arithmetic operations or values, and those that don't often have other concepts. In Common Lisp, `nil` or `()` is false, and everything else is true. In Perl, `""` is one possible false value.
David Thornley
+1  A: 

it is an infinite for loop.

GK
+7  A: 

It is an infinite loop which has no initial condition, no increment condition and no end condition. So it will iterate forever equivalent to while(1).

M. Atif Riaz
at least one compiler I know will warn about while ( 1 ) or while ( true ) loops, but "know" to not produce diagnostics for ( ; ; ) as it is considered the "standard" form for infinite loop.
Peeter Joot
+1  A: 

It is same as writing infinite loop using " for " statement but u have to use break or some other statement that can get out of this loop.

Swapnil
+1  A: 

I means:

#define EVER ;;

for(EVER)
{
     // do something
}

Warning: Using this in your code is highly discouraged.

JCasso
I really hate it when I see code like this. The author obviously think he is more clear but I often have to back and make sure that EVER isn't defined as something bad that will cause me problem.
Fredrik
True, i would not write for(EVER) in my code either. This is just for fun :)
JCasso
Indeed, don't do this in real code.
Ree
+14  A: 

The for(;;) is an infinite loop condition, similar to while(1) as most have already mentioned. You would more often see this, in kernel mutex codes, or mutex eg problem such as dining philosophers. Until the mutex variable is set to a particular value, such that a second process gets access to the resource, the second process keeps on looping, also known as busy wait. Access to a resource can be disk access, for which 2 process are competing to gain access using a mutex such that at a time only one process has the access to the resource.

Abhijit K Rao
A: 

that was obviously infinite loop condition.

deddihp