Professional Web Applications Themes

drawing in threads - Mac Programming

(cross posted from comp.lang.objective-c) So, I have a multi-threaded application (a simple game, ftr), and it's handling its animation in a separate thread. So user input changes game state, and a second thread in a loop notices that and starts operating as long as the state is alterred. Everything's peachy. Except for the fact that I just discovered that the actual re-drawing of my view is happening in the main thread of execution, instead of the separate animation thread. I'm invoking the re-draw via a display call, as such: (in controller) -(void) animate { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; ...

  1. #1

    Default drawing in threads

    (cross posted from comp.lang.objective-c)

    So, I have a multi-threaded application (a simple game, ftr), and it's
    handling its animation in a separate thread. So user input changes game
    state, and a second thread in a loop notices that and starts operating
    as long as the state is alterred. Everything's peachy.

    Except for the fact that I just discovered that the actual re-drawing of
    my view is happening in the main thread of execution, instead of the
    separate animation thread.

    I'm invoking the re-draw via a display call, as such:

    (in controller)

    -(void) animate {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    while (1) {
    while (moving != NOT_MOVING) {
    //do interesting things.
    [myView display];
    }
    }
    [pool release];
    }


    But, this is causing the redraw to happen in my main thread
    of execution, which is bad, because things are getting out of sync.

    What's the best way to fix this? I'm leaning towards changing the
    display
    call to:

    [myView lockFocusIfCanDraw];
    [myView drawRect:[myView bounds]];
    [myView unlockFocus];

    (and yes, I do need to re-draw the full view in this instance). But I'm
    not sure if that's proper or ideal technique. And I also don't have it
    working yet, so I know I'm missing something (hopefully obvious)

    Anybody have any suggestions? Or am I on the right track?

    Thanks,

    -Bink.
    Mike Guest

  2. #2

    Default Re: drawing in threads

    Mike Binkley <com> wrote:
     

    Have you read the docs on Multithreading? There is a very clear section
    about the appkit's non-thread-safety, and how to draw. m.

    --
    matt neuburg, phd = com, http://www.tidbits.com/matt/
    AppleScript: The Definitive Guide
    http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt Guest

  3. #3

    Default Re: drawing in threads

    > Have you read the docs on Multithreading? There is a very clear section 


    Yeah, I'd read it. But I wouldn't say that it's very clear. It spends
    most of the time talking about how it -is- threadsafe and the standard
    methods are implemented and you can run with them and be happy. It's
    buried pretty deeply where it says, "oh, by the way, drawing always
    happens on the main thread and calling display in a thread is equivalent
    to calling setNeedsDisplay:YES".

    Further, if you actually need to do your drawing in the thread instead
    of handing it off to the main thread, you have to bypass drawRect:
    entirely in order to do it. And that still just reeks of a hack to me.

    Anyway, locking the focus, drawing, and then unlocking is correct. The
    line I was missing was that I needed to explicitly flush the window when
    I was done drawing to get my changes to show up.

    -Bink.....
    Mike Guest

  4. #4

    Default Re: drawing in threads

    Mike Binkley <com> wrote:
     

    Is that a feature of threading or simply how drawing works in general?
    m.

    --
    matt neuburg, phd = com, http://www.tidbits.com/matt/
    AppleScript: The Definitive Guide
    http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt Guest

Similar Threads

  1. Why are these threads deleted so often?
    By VeganMan007 in forum Macromedia Exchange Dreamweaver Extensions
    Replies: 1
    Last Post: September 8th, 09:13 PM
  2. Drawing in Flex? Drawing and AS for custom compon
    By yoway in forum Macromedia Flex General Discussion
    Replies: 0
    Last Post: April 18th, 10:19 PM
  3. bug: threads and win32
    By anon in forum Ruby
    Replies: 10
    Last Post: December 27th, 02:09 AM
  4. threads and io
    By JB in forum Ruby
    Replies: 4
    Last Post: December 15th, 01:49 PM
  5. Threads again
    By Peteris in forum UNIX Programming
    Replies: 3
    Last Post: September 9th, 01:07 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139