multi-threading, AppKit etc

  • hi there,
    i'm having a bit of trouble with the following test code i wrote (debugger exits at the runModal: method of NSAlert even though it's called in the main thread):
    (i'm afraid it's missing all the includes/imports).
    my project required a 'main' so i call the NSApplicationMain from that - i'm hoping that's not what's causing the problem, since the test confirms that the NSAlert is in the main thread.
    any help would be much appreciated,
    thanks,
    tom

    @interface DeadThread : NSThread {;;}
    + (void)enterMultiThreadedMode;
    + (void)emptyThreadMethod:(id)obj;
    @end

    @implementation DeadThread
    + (void)enterMultiThreadedMode
    {
    [NSThread detachNewThreadSelector:@selector(emptyThreadMethod:) toTarget:[DeadThread class] withObject: nil]; // force Cocoa to enter multithreading mode.
    }
    + (void)emptyThreadMethod:(id)obj {;;}
    @end

    int NSApplicationMain(int argc, const char ** argv)
    {

    [DeadThread enterMultiThreadedMode];
    bool multi = [NSThread isMultiThreaded]; // currently returns TRUE, confirming that Cocoa is in multithreading mode.
    NSAutoreleasePool releasePool;
    const unistring mess = "error message"; // a unistring is just a typedef'd std::string that our company uses.
    const unistring title = "new error";

    NSString *message = [[NSString alloc] initWithCString:title.c_str() encoding:NSASCIIStringEncoding];
    NSString *information = [[NSString alloc] initWithCString:mess.c_str() encoding:NSASCIIStringEncoding];

    NSAlert *alertbox = [[NSAlert alloc] init];

    [alertbox setMessageText:message];
    [alertbox setInformativeText:information];
    [alertbox setAlertStyle:NSCriticalAlertStyle];
    [alertbox addButtonWithTitle:@"OK"];

    int test = pthread_main_np(); // currently returns a value of '1', confirming that the NSAlert instance is being run in the main thread as required (AppKit stuff still cannot assumed to be threadsafe otherwise?)

    return [alertbox runModal];
    }

    int main(int argc, char* argv[])
    {
    return NSApplicationMain(argc, (const char **) argv);
    }
  • On Oct 16, 2006, at 9:25 AM, Tom O'Grady wrote:

    > hi there,
    > i'm having a bit of trouble with the following test code i wrote
    > (debugger exits at the runModal: method of NSAlert even though it's
    > called in the main thread):
    > (i'm afraid it's missing all the includes/imports).
    > my project required a 'main' so i call the NSApplicationMain from
    > that - i'm hoping that's not what's causing the problem, since the
    > test confirms that the NSAlert is in the main thread.
    > any help would be much appreciated,
    > thanks,
    > tom
    >
    > int NSApplicationMain(int argc, const char ** argv)
    > {
    >
    > [DeadThread enterMultiThreadedMode];
    > bool multi = [NSThread isMultiThreaded]; // currently returns
    > TRUE, confirming that Cocoa is in multithreading mode.
    > NSAutoreleasePool releasePool;
    > const unistring mess = "error message"; // a unistring is just a
    > typedef'd std::string that our company uses.
    > const unistring title = "new error";
    >
    > NSString *message = [[NSString alloc] initWithCString:title.c_str
    > () encoding:NSASCIIStringEncoding];
    > NSString *information = [[NSString alloc]
    > initWithCString:mess.c_str() encoding:NSASCIIStringEncoding];
    >
    > NSAlert *alertbox = [[NSAlert alloc] init];
    >
    > [alertbox setMessageText:message];
    > [alertbox setInformativeText:information];
    > [alertbox setAlertStyle:NSCriticalAlertStyle];
    > [alertbox addButtonWithTitle:@"OK"];
    >
    > int test = pthread_main_np(); // currently returns a value of '1',
    > confirming that the NSAlert instance is being run in the main
    > thread as required (AppKit stuff still cannot assumed to be
    > threadsafe otherwise?)
    >
    > return [alertbox runModal];
    > }
    >
    > int main(int argc, char* argv[])
    > {
    > return NSApplicationMain(argc, (const char **) argv);
    > }

    1) ...you have me confused... "NSApplicationMain" is a function
    provided by the AppKit Framework but in the above you appear to be
    implementing your own? Can you explain what is taking place.

    2) No need to use your DeadThread object, just remove it from your code.

    3) In the above code example you are not getting AppKit/Cocoa
    initialized and connected with the window server (assuming the real
    NSApplicationMain isn't being called). For this code sample try
    adding in NSApplicationLoad(); as the first thing in your main method.

    -Shawn
  • On 17/10/06, Tom O'Grady <tom.ogrady...> wrote:
    > NSAutoreleasePool releasePool;

    I have no idea what's going on, but I notice that this isn't being
    alloc'd and init'd (or released) anywhere. Since you seem to be taking
    matters into your own hands, this should be one of the first things
    you do.

    -Phil
  • > [NSThread detachNewThreadSelector:@selector(emptyThreadMethod:)
    > toTarget:[DeadThread class] withObject: nil]; // force Cocoa to
    > enter multithreading mode.

    In detachNewThreadSelector,  toTarget: expects id argument, not class
    structure.

    On 16-Oct-06, at 9:55 PM, Tom O'Grady wrote:

    > hi there,
    > i'm having a bit of trouble with the following test code i wrote
    > (debugger exits at the runModal: method of NSAlert even though it's
    > called in the main thread):
    > (i'm afraid it's missing all the includes/imports).
    > my project required a 'main' so i call the NSApplicationMain from
    > that - i'm hoping that's not what's causing the problem, since the
    > test confirms that the NSAlert is in the main thread.
    > any help would be much appreciated,
    > thanks,
    > tom
    >
    > @interface DeadThread : NSThread {;;}
    > + (void)enterMultiThreadedMode;
    > + (void)emptyThreadMethod:(id)obj;
    > @end
    >
    > @implementation DeadThread
    > + (void)enterMultiThreadedMode
    > {
    > [NSThread detachNewThreadSelector:@selector(emptyThreadMethod:)
    > toTarget:[DeadThread class] withObject: nil]; // force Cocoa to
    > enter multithreading mode.
    > }
    > + (void)emptyThreadMethod:(id)obj {;;}
    > @end
    >
    > int NSApplicationMain(int argc, const char ** argv)
    > {
    >
    > [DeadThread enterMultiThreadedMode];
    > bool multi = [NSThread isMultiThreaded]; // currently returns
    > TRUE, confirming that Cocoa is in multithreading mode.
    > NSAutoreleasePool releasePool;
    > const unistring mess = "error message"; // a unistring is just a
    > typedef'd std::string that our company uses.
    > const unistring title = "new error";
    >
    > NSString *message = [[NSString alloc] initWithCString:title.c_str
    > () encoding:NSASCIIStringEncoding];
    > NSString *information = [[NSString alloc]
    > initWithCString:mess.c_str() encoding:NSASCIIStringEncoding];
    >
    > NSAlert *alertbox = [[NSAlert alloc] init];
    >
    > [alertbox setMessageText:message];
    > [alertbox setInformativeText:information];
    > [alertbox setAlertStyle:NSCriticalAlertStyle];
    > [alertbox addButtonWithTitle:@"OK"];
    >
    > int test = pthread_main_np(); // currently returns a value of '1',
    > confirming that the NSAlert instance is being run in the main
    > thread as required (AppKit stuff still cannot assumed to be
    > threadsafe otherwise?)
    >
    > return [alertbox runModal];
    > }
    >
    > int main(int argc, char* argv[])
    > {
    > return NSApplicationMain(argc, (const char **) argv);
    > }
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/apparao%
    > 40effigent.net
    >
    > This email sent to <apparao...>
    >
  • On 17 okt 2006, at 11.46, Apparao wrote:

    >> [NSThread detachNewThreadSelector:@selector(emptyThreadMethod:)
    >> toTarget:[DeadThread class] withObject: nil]; // force Cocoa to
    >> enter multithreading mode.
    >
    > In detachNewThreadSelector,  toTarget: expects id argument, not
    > class structure.

    The class object is the appropriate target if the selector is for a
    class method.

    j o a r
previous month october 2006 next month
MTWTFSS
            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          
Go to today