Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Help with NSThread / NSLocks etc --> Bus Error
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Help with NSThread / NSLocks etc --> Bus Error



Hi all,

Before I forget, I'm on Mac OS X 10.1.5 with the Dec 2001 Dev Tools installed. But I don't imagine that this makes much difference for the below.

Over the weekend I've been dabbling with creating a Foundation Tool for experimental purposes. It's a simple thing that I did with the sole aim of seeing how things are done in the land of Objective-C / Cocoa rather than straight C / pthreads. Naturally, my straight C / pthreaded version works perfectly - but we're having a small prob with the ObjC Foundation Tool. All the experimental tool does is create a number of threads, each of which do some basic work, and after a number of seconds shuts down.

Usage: AppName numThreads numSeconds

The problems I'm having are twofold. Any pointers on the problem(s) would be greatly appreciated - or even better ways of doing things :-))

Problem #1:
A simple annoyance - Project Builder won't compile the thing :-/ It simply says build failed - check logs. So I've been compiling the tool as follows from the command line...(with no worries).


$ cc -c LDThread.m
$ cc -framework Foundation -lobjc main.m *.o -o LDThreadTest

So what's up with Project Builder? I've tried dumping the build directory to no avail.
(btw, if I put the whole code in a single file, then project builder will do it - go figure).


Do I need to use a _REENTRANT flag when using NSThreads - or something else?
If I try to compile with -D_REENTRANT I get compilation errors...
[localhost:Coding/Cocoa/LDThreadTest] ldeck% cc -D_REENTRANT -framework Foundation -lobjc main.m *.o -o LDThreadTest
main.m:7: warning: could not use precompiled header '/System/Library/Frameworks/Foundation.framework/Headers/Foundation.p', because:
main.m:7: warning: #ifdef '_REENTRANT' not defined when precompiled, but now defined:
main.m:7: warning: on the command line
/System/Library/Frameworks/Foundation.framework/Headers/NSAppleEventDescriptor.
h:6: warning: could not use precompiled header '/System/Library/Frameworks/ApplicationServices.framework/Headers/ApplicationServices.
p', because:
/System/Library/Frameworks/Foundation.framework/Headers/NSAppleEventDescriptor.
h:6: warning: #ifdef '_REENTRANT' not defined when precompiled, but now defined:
/System/Library/Frameworks/Foundation.framework/Headers/NSAppleEventDescriptor.
h:6: warning: on the command line
/System/Library/Frameworks/ApplicationServices.framework/Headers/ApplicationServices.
h:20: warning: could not use precompiled header '/System/Library/Frameworks/CoreServices.framework/Headers/CoreServices.p'
, because:
/System/Library/Frameworks/ApplicationServices.framework/Headers/ApplicationServices.
h:20: warning: #ifdef '_REENTRANT' not defined when precompiled, but now defined:
/System/Library/Frameworks/ApplicationServices.framework/Headers/ApplicationServices.
h:20: warning: on the command line
[localhost:Coding/Cocoa/LDThreadTest] ldeck%



Problem #2:
Just prior to the threads all finishing up (along with the main thread)... I sometimes get a bus error reported, or a segmentation fault, or sometimes no error at all. I've gone over the code with a fine tooth comb and I'm fairly certain that everything's in order, so I don't know what's causing the error.


To my understanding NSLog is on the thread safe list.

Example output....
[localhost:Coding/Cocoa/LDThreadTest] ldeck% ./LDThreadTest 4 1
2003-02-16 23:33:24.221 LDThreadTest[2156] Thread[1] with Total: 1
2003-02-16 23:33:24.223 LDThreadTest[2156] Thread[2] with Total: 2
2003-02-16 23:33:24.227 LDThreadTest[2156] Thread[3] with Total: 3
2003-02-16 23:33:24.230 LDThreadTest[2156] Thread[4] with Total: 4
...
2003-02-16 23:33:24.244 LDThreadTest[2156] Thread[4] with Total: 16
2003-02-16 23:33:24.247 LDThreadTest[2156] Thread[1] with Total: 16
2003-02-16 23:33:24.250 LDThreadTest[2156] Thread[2] with Total: 32
...
2003-02-16 23:33:25.027 LDThreadTest[2156] Thread[1] with Total: 10
2003-02-16 23:33:25.027 LDThreadTest[2156] Thread[1] with Total: 10
2003-02-16 23:33:25.437 LDThreadTest[2156] Thread[1] exiting...
Bus error
[localhost:Coding/Cocoa/LDThreadTest] ldeck%


#import <Foundation/Foundation.h> #import "LDThread.h"

NSLock * total_lock;
NSConditionLock * cond_lock;
long total;

@interface LDThread (Helper)
+ (void)workerThread:(id)anObject;
@end

@implementation LDThread (Helper)
+ (void)workerThread:(id)anObject
{
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	LDThread * test = [LDThread instance: (id)anObject];
	unsigned condition;
	long temp;

	while (! [test intervalHasExpired]) {
		[total_lock lock];
		temp = total;
		[total_lock unlock];

		temp = [test calculate: temp];

		[total_lock lock];
		total = temp;
		[total_lock unlock];

		NSLog(@"Thread[%d] with Total: %d", [test instanceIdentity], temp);
	}
	NSLog(@"Thread[%d] exiting...", [test instanceIdentity]);

	// synchronize with main thread
	[cond_lock lock];
	condition = [cond_lock condition];
	[cond_lock unlockWithCondition:++condition];

	[test release];
	[pool release];
}
@end

int main(int argc, const char* argv[]) {
		...
		// spawn threads
		[total_lock lock];
		for (counter = 0; counter < nThreads; counter++) {
			[NSThread detachNewThreadSelector:@selector(workerThread:)
				toTarget:[LDThread self]
				withObject:(id)[threadArgs objectAtIndex: counter]];
		}
		[total_lock unlock];

		// synchronize with worker threads so that we don't lose an item
		[cond_lock lockWhenCondition:nThreads];
		[threadArgs release];
	}
	NSLog(@"Main Thread signing out...");
	...
}

Thanks again very much!!

with regards,

--
Lachlan Deck
email@hidden
_______________________________________________
studentdev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/studentdev
Do not post admin requests to the list. They will be ignored.



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.