0

Cause NSThread can't be joinable I tried next method, it seems works ok, but is still very bad solution or good enough?

// init thread
NSThread *mythread = [[NSThread alloc] initWithTarget:self selector:@selector(runThread:) object: nil];

// start thread
mythread.start;

// JOIN NSThread custom implementation
// wait until thread will finish execution
if (mythread.isExecuting) {
    while(mythread.isExecuting) {
        sleep(0);
    }
} else if (!mythread.isCancelled && !mythread.isFinished) {
    while(!mythread.isExecuting) {
        sleep(0);
    }
    while(mythread.isExecuting) {
        sleep(0);
    }
}
user924
  • 8,146
  • 7
  • 57
  • 139
  • Maybe using Grand Central Dispatch could be an alternative. This provides everything you could ever think of :-) – Andreas Oetjen Mar 12 '18 at 11:35
  • @AndreasOetjen `NSThread` just looks easier and it's similar to **Java** `Thread`, mb you could write some examples on the next question? https://stackoverflow.com/questions/49148774/implementing-next-threading-solutions-in-swift-objective-c – user924 Mar 12 '18 at 11:42
  • I would suggest you take a look yourself; there are several tutorials regarding GCD, or maybe NSOperation. Ranking from low- to high level, there are posix threads and `NSThread` (which are quite basic), then GCD and highest: `NSOperation` (which itself is based upon GCD). – Andreas Oetjen Mar 12 '18 at 12:12
  • @AndreasOetjen for the first solution I got it how to do it https://stackoverflow.com/a/49235104/7767664 now I need find out how to implement the second one – user924 Mar 12 '18 at 12:21
  • @AndreasOetjen for second one I fount this solution https://stackoverflow.com/a/13033373/7767664 but it's ugly – user924 Mar 12 '18 at 12:26
  • I think you should do this with NSOperation, which has similar properties. By the way, your code does not seem to be thread-safe at all, so you will run into problems soon (especially with concurrent access of global/static variables). – Andreas Oetjen Mar 12 '18 at 12:29
  • @AndreasOetjen which code isn't safe? – user924 Mar 12 '18 at 12:30
  • The code in https://stackoverflow.com/questions/49148774/implementing-next-threading-solutions-in-swift-objective-c/49235104#49235104 – Andreas Oetjen Mar 12 '18 at 12:32
  • @AndreasOetjen I just found example with similar code and I don't think it can be written differently with `dispatch_group_t` – user924 Mar 12 '18 at 12:33
  • @AndreasOetjen of course if I want to access class variable from different threads I need to make sure that such variable is thread-safe for example Java has https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentMap.html – user924 Mar 12 '18 at 12:35
  • @AndreasOetjen anyway those two blocks will process different data (different variables), so it's just another question (my own problem how I would use my data), and I don't talking about it here, only about threads – user924 Mar 12 '18 at 12:36
  • @AndreasOetjen so `dispatch_group_async` or `dispatch_queue_async` doesn't have methods like isRunning, isAlive or isExecution, but `NSOperation` has? – user924 Mar 12 '18 at 12:39
  • You'll likely find more success by not trying to force Objective-C threading to act like Java threading. Use GCD for your concurrency model in iOS and OS X and you're app will work much better with the system. – bbum Mar 12 '18 at 17:09

1 Answers1

1

A live lock like this is a bad idea on an iPhone, because it eats battery and CPU without doing anything, even though calling sleep(0) might give it a little bit of rest.

You could use NSCondition to implement joining. The idea is that the parent thread will wait on the NSCondition, and the worker thread would signal on that condition when it finishes:

- (void)main1 {
    // thread 1: start up
    _joinCond = [NSCondition new];
    [mythread start];

    // thread 1: join, i.e. wait until thread 2 finishes
    [_joinCond lock];
    [_joinCond wait];
    [_joinCond unlock];
}

- (void)main2 {
    // thread 2 (mythread):
    // ... work, work, work ...
    // now we're done, notify:
    [_joinCond lock];
    [_joinCond signal];
    [_joinCond unlock];
}
battlmonstr
  • 5,841
  • 1
  • 23
  • 33