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.
9 #include <Foundation/Foundation.h>
12 static pthread_mutex_t test_mutex;
14 static void Mutex_Init (void)
16 pthread_mutexattr_t tmp_mutex_attr;
17 pthread_mutexattr_init(&tmp_mutex_attr);
18 pthread_mutex_init(&test_mutex, &tmp_mutex_attr);
21 @interface LockMe :NSObject
25 - (NSString *) description;
28 @implementation LockMe
29 - (NSString *) description
31 printf ("LockMe trying to get the lock.\n");
32 pthread_mutex_lock(&test_mutex);
33 printf ("LockMe got the lock.\n");
34 pthread_mutex_unlock(&test_mutex);
35 return @"I am pretty special.\n";
40 my_pthread_routine (void *data)
42 printf ("my_pthread_routine about to enter.\n");
43 pthread_mutex_lock(&test_mutex);
44 printf ("Releasing Lock.\n"); // Set a breakpoint here.
45 pthread_mutex_unlock(&test_mutex);
52 pthread_attr_t tmp_attr;
53 pthread_attr_init (&tmp_attr);
58 LockMe *lock_me = [[LockMe alloc] init];
59 pthread_create (&my_pthread, &tmp_attr, my_pthread_routine, NULL);
61 pthread_join (my_pthread, NULL);