本文整理汇总了C++中Context::DestroyAllThreads方法的典型用法代码示例。如果您正苦于以下问题:C++ Context::DestroyAllThreads方法的具体用法?C++ Context::DestroyAllThreads怎么用?C++ Context::DestroyAllThreads使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Context
的用法示例。
在下文中一共展示了Context::DestroyAllThreads方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main( int argc, char* argv[])
{
// create the context on the stack
Context ct;
// init the context
ct.Init();
// set the run state
ct.m_Running = true;
// create the input thread
ct.m_InputThread = ct.MakeThread(InputThread);
ct.m_InputThread->m_Type = LocalContext::T_INPUT;
bool success = true;
// create the TCP thread
ct.m_TCPThread = ct.MakeThread(TCPRecvThread);
ct.m_TCPThread->m_Sockfd = -1;
ct.m_TCPThread->m_Type = LocalContext::T_TCP;
ct.m_TCPThread->m_Mutex = ct.m_TCPMutex;
/*
// create the UDP thread
ct.m_TCPThread = ct.MakeThread(RecvThread);
ct.m_TCPThread->m_Sockfd = -1;
ct.m_TCPThread->m_Type = LocalContext::T_TCP;
*/
// create the TTY thread
ct.m_TTYThread = ct.MakeThread(TTYRecvThread);
ct.m_TTYThread->m_Sockfd = -1;
ct.m_TTYThread->m_Type = LocalContext::T_TTY;
ct.m_TTYThread->m_Mutex = ct.m_TTYMutex;
LogMsgToTerminal("INITIALIZATION COMPLETE");
// start the loop
pthread_mutex_lock(&(ct.m_PollMutex));
while( ct.m_Running )
{
// wait on the condition
pthread_cond_wait(&(ct.m_PollCondition), &(ct.m_PollMutex));
// access the thread context
LocalContext* tLct = ct.FindThread( ct.m_Caller );
if( tLct == NULL )
continue; // no thread context...
// do work
stringstream ss;
// check the caller
if( ct.m_Caller == 1 ) // called from the input thread
{
// simple: broadcast the input thread string to the server
// make sure we have a valid connection, and
// this was not an exit command
if( success && ct.m_Running )
{
// send the message
//LogMsgToTerminal( tLct->m_MSG );
//size_t sent = send( sockfd, tLct->m_MSG.c_str(), tLct->m_MSG.length(), 0 );
}
}
else // called from recv thread
{
// the recv threads
// simply log the message to the screen
LogMsgToTerminal(tLct->m_MSG);
}
}
pthread_mutex_unlock(&(ct.m_PollMutex));
// clean up the socket
if( (ct.m_TCPThread)->m_Sockfd >= 0 )
{
close((ct.m_TCPThread)->m_Sockfd);
}
// destroy and join the threads
ct.DestroyAllThreads();
//.........这里部分代码省略.........
示例2: main
//.........这里部分代码省略.........
LocalContext* newCT = ct.MakeThread(RecvThread);
newCT->m_Sockfd = sockfd;
}
}
}
// start the loop
pthread_mutex_lock(&(ct.m_PollMutex));
while(ct.m_Running)
{
// wait on the condition
pthread_cond_wait(&(ct.m_PollCondition), &(ct.m_PollMutex));
// access the thread context
LocalContext* tLct = ct.FindThread( ct.m_Caller );
if( tLct == NULL )
continue; // no thread context...
// do work
stringstream ss;
// check the caller
if( ct.m_Caller == 1 )
{
// the input thread
// switch on mode
switch( ct.m_Mode )
{
case 1: // server
// broadcast to all recv threads
ss << ct.m_DPort << ": " << tLct->m_MSG;
ct.BroadcastAll( ss.str() );
break;
case 2: // client
// simple: broadcast the input thread string to the server
// make sure we have a valid connection, and
// this was not an exit command
if( success && ct.m_Running )
{
// send the message
LogMsgToTerminal( tLct->m_MSG );
size_t sent = send( sockfd, tLct->m_MSG.c_str(), tLct->m_MSG.length(), 0 );
}
break;
}
}
else
{
// the recv threads
// switch on mode
switch( ct.m_Mode )
{
case 1: // server
// log the message and transmit
ss << GetPortFromStruct( &(tLct->m_Addr) );
ss << ": " << tLct->m_MSG;
LogMsgToTerminal(ss.str());
ct.BroadcastAll( ss.str(), ct.m_Caller );
break;
case 2: // client
// simply log the message to the screen
LogMsgToTerminal(tLct->m_MSG);
break;
}
}
}
pthread_mutex_unlock(&(ct.m_PollMutex));
// clean up the socket
if( ct.m_Mode == 2 )
{
if( success )
{
close(sockfd);
}
}
// destroy and join the threads
ct.DestroyAllThreads();
// shut down the context
ct.Shutdown();
return 0;
}