Skip to content

Instantly share code, notes, and snippets.

@kiding
Created January 17, 2012 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiding/1627163 to your computer and use it in GitHub Desktop.
Save kiding/1627163 to your computer and use it in GitHub Desktop.
Sleep Sort - Objective-C (Cocoa)
/* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2012 Dongsung "Don" Kim <kiding@me.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
*/
#import <Foundation/Foundation.h>
@interface SortControl : NSObject
- (void) sleepForTime:(id)param;
@end
@implementation SortControl
- (void) sleepForTime:(NSNumber *)param {
usleep([param intValue] * 1000000);
NSLog(@"%d", [param intValue]);
}
@end
int main (void)
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int argv[9] = {1, 5, 7, 6, 2, 9, 8, 10, 13};
int argc = 9;
SortControl *control = [[[SortControl alloc] init] autorelease];
NSMutableArray *threads = [NSMutableArray array];
for(int i=0; i<argc; i++) {
NSNumber *inputNumber = [NSNumber numberWithInt:argv[i]];
NSInvocationOperation *thisThread = [[[NSInvocationOperation alloc]
initWithTarget:control
selector:@selector(sleepForTime:)
object:inputNumber]
autorelease];
[threads addObject:thisThread];
}
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperations:threads waitUntilFinished:YES];
[pool drain];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment