void testThreadInitAndExit() { std::cout<<"****** Running thread start and delete test ****** "<<std::endl; { MyThread thread; thread.startThread(); } // add a sleep to allow the thread start to fall over it its going to. OpenThreads::Thread::microSleep(500000); std::cout<<"pass thread start and delete test"<<std::endl<<std::endl; std::cout<<"****** Running notify thread test ****** "<<std::endl; { NotifyThread thread1(osg::INFO,"thread one:"); NotifyThread thread2(osg::INFO,"thread two:"); NotifyThread thread3(osg::INFO,"thread three:"); NotifyThread thread4(osg::INFO,"thread four:"); thread1.startThread(); thread2.startThread(); thread3.startThread(); thread4.startThread(); // add a sleep to allow the thread start to fall over it its going to. OpenThreads::Thread::microSleep(5000000); } std::cout<<"pass noitfy thread test."<<std::endl<<std::endl; }
void* MyThread::the_thread(void * param) { MyThread *thread; void *ret; thread = (MyThread*)param; thread->continue_mtx2_.lock(); thread->mutex_.lock(); thread->running_ = true; thread->mutex_.unlock(); thread->continue_mtx_.lock(); thread->continue_mtx_.unlock(); ret = thread->thread(); thread->mutex_.lock(); thread->running_ = false; thread->retval_ = ret; thread->mutex_.unlock(); return NULL; }
void MyServer::incomingConnection(int socketDescriptor) { qDebug() << socketDescriptor << " Connecting..."; MyThread *thread = new MyThread(socketDescriptor,this); connect(thread, SIGNAL(finished()),thread, SLOT(deleteLater())); thread->start(); }
extern "C" void * Thread(void *arg) { MyThread *me = (MyThread *)arg; int n; unsigned int t, s = (int)arg; char b[100]; time_t t1, t2; me->print("start"); for (n = 1; ;++n) { t = rand_r(&s) % 4; t1 = time(NULL); fl.lock(me); t2 = time(NULL); sprintf(b, "lock %03d at %d, start wait at %d", n, t2, t1); me->print(b); if (t) sleep(t); fl.unlock(); t = rand_r(&s) % 4; if (t) sleep(t); } return 0; }
void MyServer::incomingConnection(qintptr socketDescriptor){ qDebug() << socketDescriptor << " connecting..."; MyThread *thread = new MyThread(socketDescriptor,this, &storage); connect(thread,SIGNAL(finished()), thread, SLOT(deleteLater())); thread->run(); }
void* ThreadFunc( void* _pParam ) { MyThread* pMyThread = static_cast<MyThread*>( _pParam ); if( pMyThread ) int nRes = pMyThread->Run(); return pMyThread; }
void MyObject::start() { QTimer *timer = new QTimer(this); timer->setInterval(1000); connect(timer, SIGNAL(timeout()), SLOT(DoSomething())); MyThread *t = new MyThread(this); connect(t, SIGNAL(finished()), SLOT(onFinished())); connect(t, SIGNAL(finished()), timer, SLOT(start())); t->start(); }
int TcpServThr::WaitAllThr() { vector<MyThread*>::iterator it = ThrSet->begin(); while (it != ThrSet->end()) { MyThread* thr = (MyThread*)(*it); pthread_join(thr->getId(),NULL); it++; } return 0; }
void unlock() { MyThread *w = 0; Pthread_mutex_lock(&m); locked = 0; w = q; Pthread_mutex_unlock(&m); if (w) w->wakeup(); }
TEST( Thread, freeOnTerminateTest ) { static const int COUNT = 5; for (int i = 0; i < COUNT; i++) { MyThread* myThread = new MyThread(i); myThread->freeOnTerminate = true; EXPECT_TRUE( myThread->open() ); } sleep(1); }
void MyFrame::evhSendMsg( wxCommandEvent& WXUNUSED(event) ) { long victim = m_listCtrl1->GetSelected(); if (victim != -1){ MyThread *pThr = vciMailingList[m_listCtrl1->GetItemData(victim)]; unsigned long TID = pThr->getID(); wxMutexLocker ml(vciMailingList[TID]->mtxMail); vciMailingList[TID]->semMail.Post(); vciMailingList[TID]->lstMail.push_back(new wxString(wxT("SYSTEM;")+m_textCtrl2->GetValue())); } }
int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); MyThread mThread; mThread.start(); mThread.wait(); qDebug() << "Done!"; return a.exec(); }
void MyFrame::evhTerminateThread( wxCommandEvent& WXUNUSED(event) ) { m_checkBox1->SetValue(false); m_button1->Enable( false ); long victim = m_listCtrl1->GetSelected(); if (victim != -1){ MyThread *pThr = vciMailingList[m_listCtrl1->GetItemData(victim)]; if(pThr) pThr->Delete(); } }
void MyFrame::OnStartThread(wxCommandEvent& WXUNUSED(event) ) { MyThread *thread = CreateThread(); if ( thread->Run() != wxTHREAD_NO_ERROR ) { wxLogError(wxT("Can't start thread!")); } #if wxUSE_STATUSBAR SetStatusText(wxT("New thread started."), 1); #endif // wxUSE_STATUSBAR }
SexWindow::SexWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui_MainWindow) { ui->setupUi(this); MyThread *mythread = new MyThread; /** 元类型的注册方法 **/ int id = qRegisterMetaType<PEOPLE>("PEOPLE"); connect(mythread, SIGNAL(changeText(PEOPLE)), this, SLOT(labelSetText(PEOPLE)),Qt::QueuedConnection); mythread->start(); }
MyThread *MyFrame::CreateThread() { MyThread *thread = new MyThread; if ( thread->Create() != wxTHREAD_NO_ERROR ) { wxLogError(wxT("Can't create thread!")); } wxCriticalSectionLocker enter(wxGetApp().m_critsect); wxGetApp().m_threads.Add(thread); return thread; }
int main(int argc, char **argv) { printf("Starting...\n"); MyThread *mt = new MyThread; mt->Initialize(1024); if (!mt->Start(MyThread::Run)) do printf("Thread yielded...\n"); while (!ResumeThread(mt)); printf("Done!\n"); return 0; }
unsigned int __stdcall _MyThreadFunction(void* arg) { MyThread* pThread = static_cast<MyThread*>(arg); try { pThread->dwResult = pThread->Process(pThread->hStopSignalPtr.get()); } catch(std::exception&) { pThread->dwResult = ~(DWORD)0; } return 0; }
bool MyFrame::CreateThread(MyFrame *m_frame, const wxString&sFPath, unsigned long &TID){ wxString sScript; if (!LoadScript(sFPath, sScript) )return false; wxString sFName = wxFileName(sFPath).GetFullName(); MyThread *thread = new MyThread(m_frame, sScript, sFName, sFPath); if ( thread->Create() != wxTHREAD_NO_ERROR ){ wxLogError(wxT("Can't create thread!")); return false; } TID = vciMailingList.insert(thread); thread->setID(TID); m_listCtrl1->AddItem(sFName, TID); return true; }
int svc () { if (openfile_) { ACE_LOG_MSG->msg_ostream ( new std::ofstream ( ACE_TEXT_ALWAYS_CHAR ( ACE_LOG_DIRECTORY ACE_TEXT ("Log_Thread_Inheritance_Ostream") ACE_LOG_FILE_EXT_NAME ) ), true); ACE_LOG_MSG->set_flags (ACE_Log_Msg::OSTREAM); ACE_LOG_MSG->clr_flags (ACE_Log_Msg::STDERR | ACE_Log_Msg::LOGGER); MyThread ends_first_thread; ends_first_thread.activate (THREAD_DEFAULTS, 10); ends_first_thread.wait (); childthread_.activate (THREAD_DEFAULTS, 10); } ACE_DEBUG ((LM_DEBUG, "(%P|%t) - in svc() openfile_ is %C\n", (openfile_ ? "true" : "false"))); if (!openfile_) { for (int i = 0; i < 100; ++i) { ACE_DEBUG ((LM_DEBUG, "(%P|%t) - loop %d\n", i)); if (!(i % 10)) ACE_OS::thr_yield (); } } return 0; }
void AtomicTestCase::TestWithThreads(int count, ETestType testType) { wxAtomicInt int1=0; wxArrayThread threads; int i; for ( i = 0; i < count; ++i ) { ETestType actualThreadType; switch(testType) { default: actualThreadType = testType; break; case IncOnly: actualThreadType = (i&1)==0 ? IncOnly : DecOnly; break; } MyThread *thread = new MyThread(int1, actualThreadType); if ( thread->Create() != wxTHREAD_NO_ERROR ) { wxLogError(wxT("Can't create thread!")); } else threads.Add(thread); } for ( i = 0; i < count; ++i ) { threads[i]->Run(); } for ( i = 0; i < count; ++i ) { // each thread should return 0, else it detected some problem CPPUNIT_ASSERT (threads[i]->Wait() == (wxThread::ExitCode)0); } CPPUNIT_ASSERT( int1 == 0 ); }
int run_main (int, ACE_TCHAR *[]) { ACE_START_TEST (ACE_TEXT ("Log_Thread_Inheritance_Test")); ACE_DEBUG ((LM_DEBUG, "(%P|%t) - in run_main()\n")); ACE_DEBUG ((LM_DEBUG, "(%P|%t) - this test will crash ACE if it does not " "have the fix for bug #3480.\n")); ACE_OSTREAM_TYPE *initial_stream = ACE_LOG_MSG->msg_ostream (); MyThread mt (true); mt.activate (); mt.wait (); MyThread::childthread_.wait (); ACE_LOG_MSG->msg_ostream (initial_stream, 0); #ifdef ACE_HAS_PTHREADS if (!test_inherited_attributes ()) return -1; #endif /* ACE_HAS_PTHREADS */ ACE_DEBUG ((LM_DEBUG, "(%P|%t) - Test passed.\n")); ACE_END_TEST; return 0; }
int main() { //Just use the stack here why not MyThread Thread; Thread.Launch(); for (int i = 0; i < 10; ++i) { std::cout << "MAIN THREAD" << std::endl; sf::Sleep(1.5f); //Proof that threads don't block } Thread.Terminate(); //Properly terminate thread return EXIT_SUCCESS; //Thread is deleted after going out of scope }
void lock(MyThread *me) { Pthread_mutex_lock(&m); if (locked || q) { if (q) qEnd->setNext(me); else q = me; me->setNext(0); qEnd = me; while (locked || q != me) me->wait(&m); q = q->getNext(); if (!q) qEnd = 0; } locked = 1; Pthread_mutex_unlock(&m); }
extern "C" int main(int argc, char **argv) { char buffer[32]; const char *cp; MyThread *thr = new MyThread(); thr->start(); for(;;) { cin.getline(buffer, sizeof(buffer)); if(!strnicmp(buffer, "bye", 3)) break; cp = buffer; while(*cp) thr->post((void *)(cp++), 0); cout << "post complete!" << endl; } delete thr; return 0; }
int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); MyThread mThread; qDebug()<<"GUI thread before MyThread start()" <<a.thread()->currentThread(); qDebug()<<"GUI thread after start()" <<a.thread()->currentThread(); mThread.start(); mThread.wait(); qDebug()<<"GUI thread after wait()" <<a.thread()->currentThread(); return a.exec(); }
void RfCommClient::error(QBluetoothSocket::SocketError err) { qDebug() << __PRETTY_FUNCTION__ << "Got socket error" << err; // remote side has closed the socket, effectively disconnecting it if (state == pendingConnections) { state = dataTransfer; emit disconnected(); stopClient(); // now reconnect and send text string MyThread mythread; mythread.sleep(5); startClient(serviceInfo); connect(&lagTimer, SIGNAL(timeout()), this, SLOT(sendText())); lagTimer.start(); } else { qDebug() << __PRETTY_FUNCTION__ << "emitting done"; emit done(); } }
int main() { // drop out debug messages in_port.Register("/test/demo01/in"); out_port.Register("/test/demo01/out"); out_port.Connect("/test/demo01/in"); //YARPTime::DelayInSeconds(10); MyThread th; th.Begin(); for (int i=0; i<10; i++) { printf("Sending number %d\n", i); out_port.Content() = i; out_port.Write(); YARPTime::DelayInSeconds(1); } in_port.Unregister(); out_port.Unregister(); return 0; }
// this function creates the given number of threads and posts msgCount // messages to the last created thread which, in turn, posts all the messages // it receives to the previously created thread which does the same and so on // in cascade -- at the end, each thread will have received all msgCount // messages directly or indirectly void QueueTestCase::TestReceive() { const int msgCount = 100; const int threadCount = 10; ArrayThread threads; int i; for ( i = 0; i < threadCount; ++i ) { MyThread *previousThread = i == 0 ? NULL : threads[i-1]; MyThread *thread = new MyThread(WaitInfinitlyLong, previousThread, msgCount); CPPUNIT_ASSERT_EQUAL ( thread->Create(), wxTHREAD_NO_ERROR ); threads.Add(thread); } for ( i = 0; i < threadCount; ++i ) { threads[i]->Run(); } MyThread* lastThread = threads[threadCount - 1]; for ( i = 0; i < msgCount; ++i ) { lastThread->GetQueue().Post(i); } for ( i = 0; i < threadCount; ++i ) { // each thread should return the number of messages received. // if it returns a negative, then it detected some problem. wxThread::ExitCode code = threads[i]->Wait(); CPPUNIT_ASSERT_EQUAL( code, (wxThread::ExitCode)wxMSGQUEUE_NO_ERROR ); } }
void MyServer::incomingConnection(qintptr socketDescriptor) { // We have a new connection qDebug() << socketDescriptor << " Connecting..."; MyThread *thread = new MyThread(socketDescriptor, this); // connect signal/slot // once a thread is not needed, it will be beleted later connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); connect(thread, SIGNAL(auth(QString , QString )), this, SLOT(authUsuario(QString , QString ))); //Slot autenticacion connect(this, SIGNAL(clienteDenegado()),thread, SLOT(expulsarCliente())); //Slot expulsarCliente connect(thread, SIGNAL(registrar(QString , QString )), this, SLOT(regisUsuario(QString , QString ))); //Slot registrar connect(this, SIGNAL(notifaciones(QString)), thread, SLOT(llegadaNotificacion(QString))); //Comunicacion textual servidor-cliente connect(thread, SIGNAL(eliminarUsuario(QString)), this, SLOT(dropUser(QString))); //Slot registrar connect(thread,SIGNAL(cambiarPermisos(QString,QString,QString)),this,SLOT(actualizarPermisos(QString,QString,QString))); connect(thread,SIGNAL(removerPermisos(QString,QString,QString)),this,SLOT(quitarPermisos(QString,QString,QString))); //connect(thread,SIGNAL(sentenciaDeUsuario(QString)),this,SLOT(sentenciaNuevaUsuario(QString))); thread->start(); }