Skip to content

Instantly share code, notes, and snippets.

View hadhi631's full-sized avatar

Hadhi Abdulkareem hadhi631

  • Bangalore, India
View GitHub Profile
@hadhi631
hadhi631 / semaphore1.m
Created September 13, 2018 16:47
semaphore1
@implementation TestMutex {
dispatch_semaphore_t sem;
dispatch_queue_t queue_1;
dispatch_queue_t queue_2;
dispatch_queue_t queue_3;
dispatch_queue_t queue_4;
}
- (instancetype)init {
if (self = [super init]) {
@hadhi631
hadhi631 / Sync2Mutex.m
Created September 12, 2018 20:39
sync2mutex
@implementation TestMutex {
NSObject *mutex_1;
NSObject *mutex_2;
}
- (instancetype)init {
if (self = [super init]) {
mutex_1 = [NSObject new];
mutex_2 = [NSObject new];
}
@hadhi631
hadhi631 / syncSelf.m
Created September 12, 2018 19:45
syncSelf
@implementation TestMutex
- (void)firstMethod {
@synchronized(self) {
// Your code here
}
}
- (void)secondMethod {
@synchronized(self) {
@hadhi631
hadhi631 / RecLock.m
Created September 12, 2018 18:20
RecLock
@implementation TestMutex {
NSRecursiveLock *recLock;
}
- (instancetype)init {
if (self = [super init]) {
// Initialize the mutex
recLock = [[NSRecursiveLock alloc] init];
}
@hadhi631
hadhi631 / NSLock.m
Created September 12, 2018 15:41
NSLock
@implementation TestMutex {
BOOL isProcessing;
NSLock *lock;
}
- (instancetype)init {
if (self = [super init]) {
// Initialize the mutex
isProcessing = YES;
@hadhi631
hadhi631 / POSIXMutex.m
Created September 12, 2018 15:02
POSIX Mutex
#import <pthread/pthread.h>
@implementation TestMutex {
pthread_mutex_t mutex;
}
- (instancetype)init {
if (self = [super init]) {
// Initialize the mutex
@hadhi631
hadhi631 / personSet2.m
Created September 4, 2018 13:47
Person set2
p.firstName = @"Saddham";
p.lastName = @"Hussain";
NSString *fullName_2 = [p fullName];
@hadhi631
hadhi631 / personSet1.m
Created September 4, 2018 13:45
Person setter 1
p.firstName = @"Jack";
p.lastName = @"Daniel";
NSString *fullName_1 = [p fullName];
@hadhi631
hadhi631 / Person.m
Created September 4, 2018 13:22
Obj-C Person
@interface Person : NSObject
@property(copy) NSString *firstName;
@property(copy) NSString *lastName;
- (NSString *)fullName;
@end
@hadhi631
hadhi631 / setValue.m
Last active September 4, 2018 13:18
Atomic setter Obj-C
static inline void reallySetProperty(id self, SEL _cmd, id newValue,
ptrdiff_t offset, bool atomic, bool copy, bool mutableCopy)
{
id oldValue;
id *slot = (id*) ((char*)self + offset);
if (copy) {
newValue = [newValue copyWithZone:NULL];
} else if (mutableCopy) {
newValue = [newValue mutableCopyWithZone:NULL];