开发者

Best way to stop running pthreads using conditional variable

开发者 https://www.devze.com 2023-03-14 19:52 出处:网络
Im working with pthreads in my c code. I create and start a thread (THREAD_1) in my main routine, where from THREAD_1, I call a routine to开发者_JAVA百科 create again two new threads (THREAD_2, THREAD

Im working with pthreads in my c code. I create and start a thread (THREAD_1) in my main routine, where from THREAD_1, I call a routine to开发者_JAVA百科 create again two new threads (THREAD_2, THREAD_3). All the three thread will be running parallel with milliseconds sleep in a conditional loop. After a certain condition is met I want to stop/exit the two threads (THREAD_2, THREAD_3) and then THREAD_1. What is the best way to do this?


            while(true){
                    apr_thread_mutex_lock(mySharedObject->getMutex());
                    while (mySharedObject->getMessagesReady() == 0 && !mySharedObject->getEndThread()) {
                            apr_thread_cond_wait(mySharedObject->getCond(), mySharedObject->getMutex());
                    }
                    if (mySharedObject->getEndThread()){
                            apr_thread_mutex_unlock(mySharedObject->getMutex());
                            break;
                    }

                    apr_thread_mutex_unlock(mySharedObject->getMutex());

                    if (mySharedObject->getMessagesReady()>0){
                             myActiveProducer->send();
                    }

            }

This is the way that I've implemented it. Something wrong??

0

精彩评论

暂无评论...
验证码 换一张
取 消