PDA

View Full Version : C++ & NSThread




Gaiza
Jun 11, 2009, 03:50 PM
Hello, i want to use c++ in my iphone app (it's my main language & i can port code on other systems)

So i started a C++ class, with an NSThread as member.
Added some functions :

void WaitFoThreadToFinish();
void StartThread();
void StopThread();
static void ThreadProc(void* pOwner);

the first point is my function StartThread(), wich is implemented like this:

void CSimpleClass::StartThread()
{
if (m_pTimerThread != nil)
{
[m_pTimerThread cancel];
WaitFoThreadToFinish();
}

NSThread *driverThread = [[NSThread alloc] initWithTarget:nil selector:@selector(TimerThredProc:) object:(NSObject*)this];
m_pTimerThread = driverThread;
[driverThread release];

[m_pTimerThread start];
}

actually i pass nil as initWithTarget, i should use "this", but i get compiler error. if i set "self" i get compiler error... (not declared in this scope)

As i am e newbee with OBC, can someone explain me how to do this ?

sorry for my bad english

Thanks.



Menge
Jun 11, 2009, 03:55 PM
I don't know if you can call Objective-C code from C/C++ code.

I think you can only go from Objective-C to C/C++ and not the other way around.

kainjow
Jun 11, 2009, 04:15 PM
If you're using C++ for cross platform, why not use pthreads directly? NSThread's initWithTarget:selector:object: method takes an id for object, so if you're passing a C++ object then that will fail as NSThread might be doing a retain on the object.

gnasher729
Jun 11, 2009, 04:53 PM
Hello, i want to use c++ in my iphone app (it's my main language & i can port code on other systems)

So i started a C++ class, with an NSThread as member.
Added some functions :

void WaitFoThreadToFinish();
void StartThread();
void StopThread();
static void ThreadProc(void* pOwner);

the first point is my function StartThread(), wich is implemented like this:

void CSimpleClass::StartThread()
{
if (m_pTimerThread != nil)
{
[m_pTimerThread cancel];
WaitFoThreadToFinish();
}

NSThread *driverThread = [[NSThread alloc] initWithTarget:nil selector:@selector(TimerThredProc:) object:(NSObject*)this];
m_pTimerThread = driverThread;
[driverThread release];

[m_pTimerThread start];
}

actually i pass nil as initWithTarget, i should use "this", but i get compiler error. if i set "self" i get compiler error... (not declared in this scope)

As i am e newbee with OBC, can someone explain me how to do this ?

You are trying to mix C++ and Objective C.

First thing you have to do is save the file with the extension ".mm" which tells the compiler that it is a mix of C++ and Objective-C; without that it will complain bitterly about anything that looks like Objective-C.

Next, just for a bit of precision: Your C++ objects can't have an NSThread as a member, only an NSThread*. Different thing. Unlike C++, in Objective-C all objects are dynamically allocated.

Now where the problem is: When you initialise an NSThread object, you need to pass pointers to Objective-C objects and an Objective-C selector. C++ objects are not Objective-C objects, and casting pointers doesn't turn them into Objective-C objects. If you insist on mixing Objective-C and C++, you'd have to start with Objective-C objects. An Objective-C object can contain a pointer to a C++ object and call C++ methods.

kainjow
Jun 11, 2009, 05:00 PM
I also noticed your memory management is wrong. You create the thread, assign it to an ivar, then release the thread, but you don't retain the ivar, so that thread just deallocated and when you go to start it'll crash because the pointer wasn't reassigned to nil. You need something like:
m_pTimerThread = [driverThread retain];

Gaiza
Jun 13, 2009, 05:12 AM
So as i understand, i have to use obc as a wrapper to my c++ class, and use stl, boost or whatever into them...

Thanks a lot for your answer

Sander
Jun 13, 2009, 07:44 AM
I don't know if you can call Objective-C code from C/C++ code.

Yes, that works just fine.