0

I'm running CFRunLoopRun() in Thread A. To terminate the run loop, should I come back to Thread A to call CFRunLoopStop(CFRunLoopGetCurrent())?

What happen if I call CFRunLoopStop(CFRunLoopGetCurrent()) in Thread B? If I'm in Thread B, how could I come back to Thread A to perform this call?

Sorry for this basic question. I just switched from Android to iOS and face a lot of context switch...

Thanks!

hackjutsu
  • 8,336
  • 13
  • 47
  • 87
  • This is very low level - what are you trying to achieve? If you aren't familiar with the Cocoa frameworks then you may have missed an easier method. – Robotic Cat Oct 08 '15 at 00:22
  • Legacy codes...... Happened to jump into them... – hackjutsu Oct 08 '15 at 00:59
  • Ouch. Well here's a question where someone asked something similar and got an answer: http://stackoverflow.com/q/1363787/558933 – Robotic Cat Oct 08 '15 at 01:02
  • In what context are you calling CFRunLoopRun()? An NSThread subclass? – Charles A. Oct 08 '15 at 01:12
  • @RoboticCat I read that post. My understanding is each thread would have its own run loop. If we can get the RunLoop reference from Thread A, we can call `CFRunLoopStop(RunLoopOfThreadA)` in Thread B. But we cannot call `CFRunLoopStop(CFRunLoopGetCurrent())` since it will stop the RunLoop on Thread B if there is any. Please correct me if I miss something... – hackjutsu Oct 08 '15 at 01:18
  • @CharlesA. In some worker thread from a library we are writing... At first I used `NSRunLoop`, but I changed it to `CFRunLoopRun` since I didn't find a stop method on `NSRunLoop`. However, they seem to refer to the same object under the hood even though they cannot be cast to each other with toll free. – hackjutsu Oct 08 '15 at 01:20
  • Yeah, CFRunLoop and NSRunLoop are indeed similar. I was actually asking about what type of thread the run loop is running on. NSThread? POSIX thread? – Charles A. Oct 08 '15 at 01:22
  • @CharlesA.I believe it is POSIX thread, since most of the codes are C++. – hackjutsu Oct 08 '15 at 01:24
  • @Hackjustu Then you may want to reference that question linked above by Robotic Cat. The selected answer mentions creating a source for the run loop so that you can signal the source to get it to stop the run loop. It's discussed in a document linked from the selected answer. If it was an `NSThread`, you could use `performSelector:onThread:withObject:waitUntilDone:`, but that doesn't do any good with POSIX threads. – Charles A. Oct 08 '15 at 01:31
  • @CharlesA.Nice! It works and I just found if I use `NSRunLoop`, I don't even need to call `CFRunLoopStop`. Just trigger a method on `Thread A` then the loop will stop. – hackjutsu Oct 08 '15 at 01:39
  • @CharlesA. Would you mind putting your solution as an answer so that I can accept it? – hackjutsu Oct 08 '15 at 22:20
  • The `performSelector:onThread:withObject:waitUntilDone:` suggestion, I assume? – Charles A. Oct 09 '15 at 03:27

1 Answers1

1

If you are using POSIX threads, then you should definitely follow the link provided in the comment by @Robotic Cat. It points to a document that describes adding a source to the CFRunLoop that can be triggered to stop the run loop.

If you are using NSThread subclasses, and you have a handle to the one you want to stop, you should be able to create a method on that NSThread subclass that stops the CFRunLoop with the code you noted above and invoke it with performSelector:onThread:withObject:waitUntilDone:.

Community
  • 1
  • 1
Charles A.
  • 10,685
  • 1
  • 42
  • 39