场景描述:在程序开始阶段需要开启多个线程进行后续的处理,需要同步开启线程(即thread1启动完成后再启动thread2)
sem_t pthread_sem; // 信号量,同步线程的启动顺序
void ProcessQuery()
{
cout<<"QueryHandle, Thread "<<iCoreID<<" Create Successfully!"<<endl;
sem_post(&pthread_sem);
while(1)
{
//TODO
}
}
int main()
{
iRet = sem_init(&pthread_sem, 0, 0);
for(int j = 0; j<QUERY_PROCESS_THREAD_NUM; j++)
{
// 创建查询处理线程 线程函数为 ProcessQuery
pthread_create(&QueryHandleThread[j], NULL, ProcessQuery, NULL);
sem_wait(&pthread_sem);
}
}