What if we leave i off the by-value variables list?
size_t N, char **names, struct addrinfo **infos )
size_t i, done = 0;
sem_init( &done_sem, 0 );
for( i = 0; i < N; ++i )
assert( 0 == getaddrinfo(
names[i], NULL, NULL, &infos[i] ) );
if( ( ++done ) == N )
sem_inc( &done_sem );
sem_dec( &done_sem );
This is a pretty nasty little bug, because the code will probably work most of the time. The potential problem is the following sequence:
- One of the lookup activities yields
- The "main" activity increments i
- The lookup activity resumes and now has the wrong value for i
This kind of bug deserves some more thinking. Maybe some automatic warnings could happen.