brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.4 KiB · 03da7a7 Raw
59 lines · c
1#include <pthread.h>2#include <mach/thread_act.h>3#include <unistd.h>4 5pthread_mutex_t suspend_mutex = PTHREAD_MUTEX_INITIALIZER;6pthread_mutex_t signal_mutex = PTHREAD_MUTEX_INITIALIZER;7pthread_cond_t signal_cond = PTHREAD_COND_INITIALIZER;8 9int g_running_count = 0;10 11int12function_to_call() {13  return g_running_count;14}15 16void *17suspend_func (void *unused) {18  pthread_setname_np("Look for me");19  pthread_cond_signal(&signal_cond);20  pthread_mutex_lock(&suspend_mutex);21 22  return NULL; // We allowed the suspend thread to run23}24 25void *26running_func (void *input) {27  while (g_running_count < 10) {28    usleep (100);29    g_running_count++;  // Break here to show we can handle breakpoints30  }31  return NULL;32}33 34int35main()36{37  pthread_t suspend_thread; // Stop here to get things going38 39  pthread_mutex_lock(&suspend_mutex);40  pthread_mutex_lock(&signal_mutex);41  pthread_create(&suspend_thread, NULL, suspend_func, NULL);42 43  pthread_cond_wait(&signal_cond, &signal_mutex);44  45  mach_port_t th_port = pthread_mach_thread_np(suspend_thread);46  thread_suspend(th_port);47 48  pthread_mutex_unlock(&suspend_mutex);49 50  pthread_t running_thread;51  pthread_create(&running_thread, NULL, running_func, NULL);52  53  pthread_join(running_thread, NULL);54  thread_resume(th_port);             // Break here after thread_join55  56  pthread_join(suspend_thread, NULL);57  return 0; // Break here to make sure the thread exited normally58}59