C++ & NSThread

Discussion in 'Mac Programming' started by Gaiza, Jun 11, 2009.

  1. Gaiza macrumors newbie

    Jun 11, 2009
    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];
        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

  2. Menge macrumors 6502a


    Dec 22, 2008
    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.
  3. kainjow Moderator emeritus


    Jun 15, 2000
    If you're using C++ for cross platform, why not use pthreads directly? NSThread's initWithTarget:selector:eek:bject: 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.
  4. gnasher729 macrumors P6


    Nov 25, 2005
    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.
  5. kainjow Moderator emeritus


    Jun 15, 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:
    m_pTimerThread = [driverThread retain];
  6. Gaiza thread starter macrumors newbie

    Jun 11, 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
  7. Sander macrumors 6502

    Apr 24, 2008
    Yes, that works just fine.

Share This Page