[MacRuby] #1015: mark Cocoa as multithreaded by default

MacRuby ruby-noreply at macosforge.org
Mon Nov 29 21:53:42 PST 2010


#1015: mark Cocoa as multithreaded by default
-------------------------------------+--------------------------------------
 Reporter:  mattaimonetti@…          |       Owner:  lsansonetti@…        
     Type:  defect                   |      Status:  new                  
 Priority:  blocker                  |   Milestone:  MacRuby 0.8          
Component:  MacRuby                  |    Keywords:                       
-------------------------------------+--------------------------------------
 {{{
 >> framework 'Foundation'
 => true
 >> Thread.new{}
 => #<Thread:0x2000de8e0 run>
 >> NSThread.multiThreaded?
 => false
 >> cocoa_lock = NSThread.alloc.init
 => #<NSThread:0x200220c00>
 >> NSThread.multiThreaded?
 => false
 >> cocoa_lock.start
 => #<NSThread:0x200220c00>
 >> NSThread.multiThreaded?
 => true
 }}}

 Using MacRuby threads doesn't mark cocoa as multithreaded which could
 potentially cause issues as per the documentation:



 Protecting the Cocoa Frameworks

 For multithreaded applications, Cocoa frameworks use locks and other forms
 of internal synchronization to ensure they behave correctly. To prevent
 these locks from degrading performance in the single-threaded case,
 however, Cocoa does not create them until the application spawns its first
 new thread using the NSThread class. If you spawn threads using only POSIX
 thread routines, Cocoa does not receive the notifications it needs to know
 that your application is now multithreaded. When that happens, operations
 involving the Cocoa frameworks may destabilize or crash your application.

 To let Cocoa know that you intend to use multiple threads, all you have to
 do is spawn a single thread using the NSThread class and let that thread
 immediately exit. Your thread entry point need not do anything. Just the
 act of spawning a thread using NSThread is enough to ensure that the locks
 needed by the Cocoa frameworks are put in place.

 If you are not sure if Cocoa thinks your application is multithreaded or
 not, you can use the isMultiThreaded method of NSThread to check.

 http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/CreatingThreads/CreatingThreads.html

-- 
Ticket URL: <http://www.macruby.org/trac/ticket/1015>
MacRuby <http://macruby.org/>



More information about the macruby-tickets mailing list