Concurrency

What is the best way to print in the order for the given implementation

(void) print:(NSString*) str
while(true)
 {
    NSLog(@“%@”, str);
  }
}

[self print:@“123”];
[self print:@“ABC”];
[self print:@“456”];
[self print:@“DEF”];

output should be printing in the order continuously

123

ABC

456

DEF

123

ABC

456

DEF

What is the best way to print in the order for the given implementation

Best way in what sense ?

  • code size ?
  • speed ?
  • code cleanliness ?

Why not have the while(sure) out of func ?

(void) print:(NSString*) str {
    NSLog(@“%@”, str);
}

while(true)  {
  [self print:@“123”];
  [self print:@“ABC”];
  [self print:@“456”];
  [self print:@“DEF”];
}

We can implement using NSLock or Semaphore. So need which one is better. shouldnt hardcore the strings within while loop, that’s why l put it in a function

Concurrency
 
 
Q