Katya
Katya

Reputation: 300

Convert pthread_t to NSThread

I need check whether pthread_t is main thread. I want do it from current thread (not from pthread_t which I need check and not from main thread).

My code (pt is pthread_t)

NSThread *thread = CFBridgingRelease(pt);  // it works wrong
bool isMain = thread.isMainThread;

But I can't right convert pthread_t to NSThread.

How I can convert pthread_t to NSThread?

Or how I can check whether pthread_t is main thread?

Upvotes: 4

Views: 460

Answers (1)

John Bollinger
John Bollinger

Reputation: 181824

There is no documented way to obtain an NSThread object for a thread identified by a pthread_t, so if ...

I need check whether pthread_t is main thread.

... is really what you need to do then you are probably up a creek.

Some of the possible alternatives are

  • using NSThread instead of pthreads altogether;
  • ensuring that the main thread's pthread_t identifier is recorded somewhere where you can access it for comparison; and
  • not relying on discriminating between the main thread and others in the first place.

Note also that NSThread -- at least the one I think you mean -- is an Apple thing. If you want something more broadly portable then NSThread is a non-starter.

Upvotes: 0

Related Questions