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

Replies

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

  • In Swift 5.5, async wait could be used. You may have a look here: h t t p s : / / w w w .hackingwithswift.com/articles/233/whats-new-in-swift-5-5

Add a Comment