CoreFoundation per thread setup

  • Is there anything that has to be done to initialize CoreFoundation for
    working in a new pthread ? I'm getting crashes when calling CFSTR()
    from a spawned pthread.

    TIA
  • i have read that if using pthreads rather than NSThreads, you should
    initially spawn an NSThread then immediately get rid of it if you have
    no intention to use it, before you use your pthread as this will
    trigger some thread provisions within your cocoa app that you would
    otherwise not get

    On Monday, July 14, 2003, at 09:08  pm, David J. Clark wrote:

    > Is there anything that has to be done to initialize CoreFoundation for
    > working in a new pthread ? I'm getting crashes when calling CFSTR()
    > from a spawned pthread.
    >
    > TIA
    >
    > _______________________________________________
    > MacOSX-dev mailing list
    > <MacOSX-dev...>
    > http://www.omnigroup.com/mailman/listinfo/macosx-dev
    >
  • On Monday, July 14, 2003, at 03:08  PM, David J. Clark wrote:

    > Is there anything that has to be done to initialize CoreFoundation for
    > working in a new pthread ? I'm getting crashes when calling CFSTR()
    > from a spawned pthread.

    As mentioned, you must make sure to put Cocoa into a "safer" mode by
    detaching a single NSThread:

      [NSThread detachNewThreadSelector:nil toTarget:nil withObject:nil];

    This call will switch Cocoa into a mode containing some threading
    enhancements. You may verify that Cocoa is in threaded mode using
    [NSThread isMultiThreaded]. You should also create an NSAutoreleasePool
    for the spawned thread:

    void*
    spawn_thread(void* args)
    {
      NSAutoreleasePool* thread_pool = [NSAutoreleasePool new];

      ... put your code here ...

      [thread_pool release];
    }

    Hope this helps,
    Chris

    --
    Chris Scharver
    Electronic Visualization Laboratory
    The University of Illinois at Chicago
  • On Monday, July 14, 2003, at 18:16, Chris Scharver wrote:

    > On Monday, July 14, 2003, at 03:08  PM, David J. Clark wrote:
    >
    >> Is there anything that has to be done to initialize CoreFoundation
    >> for working in a new pthread ? I'm getting crashes when calling
    >> CFSTR() from a spawned pthread.
    >
    > As mentioned, you must make sure to put Cocoa into a "safer" mode by
    > detaching a single NSThread:

    This is not required for CoreFoundation, however, if that's all you're
    using.

    Chris Kane
    CoreFoundation, Apple
  • I'm not sure if it's applicable in your case, but keep in mind that CFSTR() isn't thread safe :)

    Cheers,
    Stefan

    > -----Original Message-----
    > From: David J. Clark [mailto:<davidjclark...>]
    > Sent: den 14 juli 2003 22:08
    > To: <macosx-dev...>
    > Subject: CoreFoundation per thread setup
    >
    >
    > Is there anything that has to be done to initialize
    > CoreFoundation for
    > working in a new pthread ? I'm getting crashes when calling CFSTR()
    > from a spawned pthread.
    >
    > TIA
    >
    > _______________________________________________
    > MacOSX-dev mailing list
    > <MacOSX-dev...>
    > http://www.omnigroup.com/mailman/listinfo/macosx-dev
    >
  • > I'm not sure if it's applicable in your case, but keep in mind that
    > CFSTR() isn't thread safe :)

    It is if you use -fconstant-cfstrings or set MACOSX_DEPLOYMENT_TARGET =
    10.2 (or higher) in the build environment.

    matt.
  • On Tuesday, Jul 15, 2003, at 10:08 US/Pacific, Stefan Johansson wrote:

    > I'm not sure if it's applicable in your case, but keep in mind that
    > CFSTR() isn't thread safe :)

    On Thursday, May 22, 2003, at 14:40 US/Pacific, Jared Watts wrote:

    > CFSTR() is thread-safe. Also if you use GCC 3 (Apples version with
    > -f-cfconstant-string or something) CFSTR becomes a builtin function
    > and the string object is created at compile time and is then
    > referenced like any other constant string.
    >
    > You can look at the source code under CoreFoundation in Darwin.

    On Monday, Dec 9, 2002, at 13:17 US/Pacific, Ali Ozer wrote:

    >> One problem may be that CFStringMakeConstantString isn't (wasn't?)
    >> thread safe. This is used by CFSTR(). You could try to serialize all
    >> of your CFSTR() calls in one place (like before all of your threads
    >> start up), or use MACOSX_DEPLOYMENT_TARGET=10.2 in the environment
    >> when building your code to use a thread-safe CFString creation
    >> mechanism.
    >
    > Actually I believe that only the calls to CFSTR() while it is
    > initializing its table are not thread-safe; doing a CFSTR() once in
    > the main thread before you start up your threads would workaround
    > this.  CFSTR() is thread-safe beyond that.  And as Matt says it should
    > be completely thread safe with MACOSX_DEPLOYMENT_TARGET=10.2, which
    > enables the "constant CFSTR" option.

    David Dunham        Macintosh Game Developer        GameHouse, Inc.
    <david...>    206 442 5881 x22    http://www.gamehouse.com
  • Hmmm... apparently I'm missing some threads in the conversation, I got only 4 or something :)

    Thanks for the info however.

    Cheers,
    Stefan

    > -----Original Message-----
    > From: David Dunham [mailto:<dunham...>]
    > Sent: den 15 juli 2003 19:44
    > To: <macosx-dev...>
    > Subject: Re: CoreFoundation per thread setup
    >
    >
    > On Tuesday, Jul 15, 2003, at 10:08 US/Pacific, Stefan Johansson wrote:
    >
    >> I'm not sure if it's applicable in your case, but keep in mind that
    >> CFSTR() isn't thread safe :)
    >
    > On Thursday, May 22, 2003, at 14:40 US/Pacific, Jared Watts wrote:
    >
    >> CFSTR() is thread-safe. Also if you use GCC 3 (Apples version with
    >> -f-cfconstant-string or something) CFSTR becomes a builtin function
    >> and the string object is created at compile time and is then
    >> referenced like any other constant string.
    >>
    >> You can look at the source code under CoreFoundation in Darwin.
    >
    > On Monday, Dec 9, 2002, at 13:17 US/Pacific, Ali Ozer wrote:
    >
    >>> One problem may be that CFStringMakeConstantString isn't (wasn't?)
    >>> thread safe. This is used by CFSTR(). You could try to
    > serialize all
    >>> of your CFSTR() calls in one place (like before all of
    > your threads
    >>> start up), or use MACOSX_DEPLOYMENT_TARGET=10.2 in the environment
    >>> when building your code to use a thread-safe CFString creation
    >>> mechanism.
    >>
    >> Actually I believe that only the calls to CFSTR() while it is
    >> initializing its table are not thread-safe; doing a CFSTR() once in
    >> the main thread before you start up your threads would workaround
    >> this.  CFSTR() is thread-safe beyond that.  And as Matt
    > says it should
    >> be completely thread safe with MACOSX_DEPLOYMENT_TARGET=10.2, which
    >> enables the "constant CFSTR" option.
    >
    >
    > David Dunham        Macintosh Game Developer        GameHouse, Inc.
    > <david...>    206 442 5881 x22    http://www.gamehouse.com
    >
    > _______________________________________________
    > MacOSX-dev mailing list
    > <MacOSX-dev...>
    > http://www.omnigroup.com/mailman/listinfo/macosx-dev
    >