| 59 | } |
| 60 | |
| 61 | void* thread::thread_run(void* arg) |
| 62 | { |
| 63 | thread* thr = (thread*) arg; |
| 64 | #ifdef ACL_WINDOWS |
| 65 | thr->thread_id_ = GetCurrentThreadId(); |
| 66 | #elif defined(ACL_FREEBSD) |
| 67 | #if defined(__FreeBSD__) && (__FreeBSD__ >= 9) |
| 68 | thr->thread_id_ = pthread_getthreadid_np(); |
| 69 | #else |
| 70 | thr->thread_id_ = (unsigned long) pthread_self(); |
| 71 | #endif |
| 72 | #elif defined(ACL_MACOSX) |
| 73 | unsigned long long n; |
| 74 | (void) pthread_threadid_np(NULL, &n); |
| 75 | thr->thread_id_ = (unsigned long) n; |
| 76 | #else |
| 77 | thr->thread_id_ = (unsigned long) pthread_self(); |
| 78 | #endif |
| 79 | |
| 80 | // �Ƚ�֮ǰ push ����������ܻ�δ�������Ϣ�����������̶߳��� |
| 81 | // �ٱ��ظ�ʹ�������߳�ʱ�������ʱ�ڴ�й¶ |
| 82 | while (true) { |
| 83 | bool found; |
| 84 | thr->sync_->pop(0, &found); |
| 85 | if (!found) { |
| 86 | break; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | thr->init(); |
| 91 | |
| 92 | thr->sync_->push(NULL); |
| 93 | |
| 94 | // ����̴߳���ʱΪ����ģʽ���� run ����ʱ�û��п��� |
| 95 | // ���̶߳��������ˣ����Բ����ٽ� thr->return_arg_ ���� |
| 96 | // ��ֵ��������п��ܳ����ڴ�Ƿ����� |
| 97 | if (thr->detachable_) { |
| 98 | return thr->run(); |
| 99 | } |
| 100 | |
| 101 | thr->return_arg_ = thr->run(); |
| 102 | return thr->return_arg_; |
| 103 | } |
| 104 | |
| 105 | bool thread::start(bool sync /* = false */) |
| 106 | { |