Problem with multiple screensaver threads

  • The question I have could be titled "How to communicate between screensaver threads?"
    Subtitle: "Are screensaver threads really threads?"

    Let me please explain the scenario (sorry for the lengthy question).

    I've got a screensaver that displays images.
    With two monitors, OSX starts two threads of my screensaver by creating two instances of my class and then calling 'animateOneFrame' for each of the instances in concurrently running loops (or so I thought!).

    Now, the problem occurs because I want each instance to show the image in Finder when the user hits the return key.

    Here is an outline of my code:

    - (void) animateOneFrame
        if ( ! we_are_on_main_screen_ )
            if ( [KeyEventHappened objectForKey: myDisplayNumber_] )
                [self keyDown: [KeyEventHappened objectForKey: myDisplayNumber_] ];
                [KeyEventHappened removeObjectForKey: myDisplayNumber_];


    - (void) keyDown: (NSEvent *) theEvent
        if ( we_are_on_main_screen_ && KeyEventHappened )
            for ( NSNumber * n in allScreenIDs_ )
                [KeyEventHappened setObject: theEvent forKey: n];
        if ( [[theEvent characters] isEqual: @"\r"] && filename_ )
            ... // show image in finder
            while ( [KeyEventHappened count] > 1 )        // wait for the other screensaver instances to process the event
                usleep( 100*1000 );
        [super keyDown: theEvent];    // will quit the screen saver, if not in preview mode (System Preferences),

    So, this is a simple producer-consumer model.

    Here, KeyEventHappened is a global dictionary that I declared as a static global variable.
    And we_are_on_main_screen_ is an instance variable that is true for the instance which is running on the main screen.
    (Apparently, only the screensaver instance on the main screen gets any keyboard events.)

    The problem now seems to be that as soon as the first instance is in the while-loop waiting for the other instances,
    'animateOneFrame' is never called for the *other* instance!

    I have found that basically by sprinkling the code with lots of printf's (err, asl_log's).

    Big question: is that really true?!

    Small question: is there an elegant way to solve this?

    All insights and suggestions will be highly appreciated.

    Best regards,
previous month february 2011 next month
  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
Go to today