bug-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: run NSApllication in an other thread


From: Julien Isorce
Subject: Re: run NSApllication in an other thread
Date: Thu, 19 Feb 2009 01:28:35 +0100



2009/2/18 Richard Frith-Macdonald <richard@tiptree.demon.co.uk>

On 17 Feb 2009, at 22:38, Julien Isorce wrote:


Unfortunately, I am in a case where I cannot manage the C main loop (I cannot configure it and I cannot start it).
I am implemting a plugin interface which lead to a shared library.
This library is loaded by the main program and I have not access to the code of this main program and so I cannot have access to the code
which runs the C main loop.
It's like the concept of plugins work in some cases. But I am sure my first description was a little bit confusing.
In other word I cannot see how the periodic function could be called in an other thread than the main one.

So I still have access to 3 functions, init, periodic func, and deinit. Each one is called in the main thread.
I have a NSWindow created in the init function.
What I need is to run the cocoa main loop ( [NSApp run] )  the handle messages of this window.
I cannot run it in one of those 3 functions ( init, periodic func, and deinit), because [NSApp run] is blocking. And I must not block
the C main loop.
So I do not know where I can run it, because it seems it's not possible to run it in an other thread than the main thread (according to the documentation).

Ah ... if you can't start the C loop yourself, so you are forced to run the NSApp loop in another thread, then you need ot make sure that the thread you run the NSApp loop in is the 'main' thread as far as GNUstep is concerned.
This means you cannot use NSThread to create the thread.
You need to use plain C code and the native threading API to create a second thread from within the init function.
In that second thread you would call GSRegisterCurrentThread() so that gnustep kows about it and would consider it the 'main' thread.

Then, once the gnustep thread is initialised as the 'main' thread, you also need to register the C loop's thread (perhaps the first time the periodic function is called) by calling GSRegisterCurrentThread() there too.

Once both threads are registered, it should be possible for the period function in the 'C' thread to call gnustep code in the 'gnustep' thread by using the preformInMainThread:... methods.

Hi,

I tried you suggestion and it seems to be fine. I have not tested the full things I need but the minimum is working.
Thx for that.

I have 2 questions:

--First, why I have to use preformInMainThread ? The 'C' thread is registered so ...
 Maybe because " [NSApplication sharedApplication] " has to be called in the second thread.
I tried to call it in the 'C' thread without any success.

--Second, "GSRegisterCurrentThread()" is a function specific to GNUstep.
And this function is not available using Apple developper environment. So I will not be able to compile my code in there.
Any comment?

Sincerely

Julien





 


reply via email to

[Prev in Thread] Current Thread [Next in Thread]