| 1 | // This file is for testing running "print object" in a case where another thread |
|---|---|
| 2 | // blocks the print object from making progress. Set a breakpoint on the line in |
| 3 | // my_pthread_routine as indicated. Then switch threads to the main thread, and |
| 4 | // do print the lock_me object. Since that will try to get the lock already gotten |
| 5 | // by my_pthread_routime thread, it will have to switch to running all threads, and |
| 6 | // that should then succeed. |
| 7 | // |
| 8 | |
| 9 | #include <Foundation/Foundation.h> |
| 10 | #include <pthread.h> |
| 11 | |
| 12 | static pthread_mutex_t test_mutex; |
| 13 | |
| 14 | static void Mutex_Init (void) |
| 15 | { |
| 16 | pthread_mutexattr_t tmp_mutex_attr; |
| 17 | pthread_mutexattr_init(attr: &tmp_mutex_attr); |
| 18 | pthread_mutex_init(mutex: &test_mutex, mutexattr: &tmp_mutex_attr); |
| 19 | } |
| 20 | |
| 21 | @interface LockMe :NSObject |
| 22 | { |
| 23 | |
| 24 | } |
| 25 | - (NSString *) description; |
| 26 | @end |
| 27 | |
| 28 | @implementation LockMe |
| 29 | - (NSString *) description |
| 30 | { |
| 31 | printf ("LockMe trying to get the lock.\n"); |
| 32 | pthread_mutex_lock(mutex: &test_mutex); |
| 33 | printf ("LockMe got the lock.\n"); |
| 34 | pthread_mutex_unlock(mutex: &test_mutex); |
| 35 | return @"I am pretty special.\n"; |
| 36 | } |
| 37 | @end |
| 38 | |
| 39 | void * |
| 40 | my_pthread_routine (void *data) |
| 41 | { |
| 42 | printf ("my_pthread_routine about to enter.\n"); |
| 43 | pthread_mutex_lock(mutex: &test_mutex); |
| 44 | printf ("Releasing Lock.\n"); // Set a breakpoint here. |
| 45 | pthread_mutex_unlock(mutex: &test_mutex); |
| 46 | return NULL; |
| 47 | } |
| 48 | |
| 49 | int |
| 50 | main () |
| 51 | { |
| 52 | pthread_attr_t tmp_attr; |
| 53 | pthread_attr_init (attr: &tmp_attr); |
| 54 | pthread_t my_pthread; |
| 55 | |
| 56 | Mutex_Init (); |
| 57 | |
| 58 | LockMe *lock_me = [[LockMe alloc] init]; |
| 59 | pthread_create (newthread: &my_pthread, attr: &tmp_attr, start_routine: my_pthread_routine, NULL); |
| 60 | |
| 61 | pthread_join (th: my_pthread, NULL); |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 |
