Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 11, 2009, 04:50 PM   #1
Gaiza
macrumors newbie
 
Join Date: Jun 2009
C++ & NSThread

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:

Code:
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.
Gaiza is offline   0 Reply With Quote
Old Jun 11, 2009, 04:55 PM   #2
Menge
macrumors 6502a
 
Menge's Avatar
 
Join Date: Dec 2008
Location: Brazil
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.
__________________
Menge - 2011 2.3GHz 13" MBP - 16GB iPhone 5 - 32GB iPad WiFi+4G - 8GB iPod Nano - AEBS
Menge is offline   0 Reply With Quote
Old Jun 11, 2009, 05:15 PM   #3
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
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.
kainjow is offline   0 Reply With Quote
Old Jun 11, 2009, 05:53 PM   #4
gnasher729
In Time-Out
 
Join Date: Nov 2005
Quote:
Originally Posted by Gaiza View Post
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:

Code:
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.
gnasher729 is offline   0 Reply With Quote
Old Jun 11, 2009, 06:00 PM   #5
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
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:
Code:
m_pTimerThread = [driverThread retain];
kainjow is offline   0 Reply With Quote
Old Jun 13, 2009, 06:12 AM   #6
Gaiza
Thread Starter
macrumors newbie
 
Join Date: Jun 2009
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
Gaiza is offline   0 Reply With Quote
Old Jun 13, 2009, 08:44 AM   #7
Sander
macrumors 6502
 
Join Date: Apr 2008
Quote:
Originally Posted by Menge View Post
I don't know if you can call Objective-C code from C/C++ code.
Yes, that works just fine.
Sander is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Forum Jump

All times are GMT -5. The time now is 08:17 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC