本文整理汇总了C++中ThreadException函数的典型用法代码示例。如果您正苦于以下问题:C++ ThreadException函数的具体用法?C++ ThreadException怎么用?C++ ThreadException使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ThreadException函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Debug
void Thread::start()
{
Debug( 1, "Starting thread" );
if ( isThread() )
throw ThreadException( "Can't self start thread" );
mThreadMutex.lock();
if ( !mStarted )
{
pthread_attr_t threadAttrs;
pthread_attr_init( &threadAttrs );
pthread_attr_setscope( &threadAttrs, PTHREAD_SCOPE_SYSTEM );
mStarted = true;
if ( pthread_create( &mThread, &threadAttrs, mThreadFunc, this ) < 0 )
throw ThreadException( stringtf( "Can't create thread: %s", strerror(errno) ) );
pthread_attr_destroy( &threadAttrs );
}
else
{
Error( "Attempt to start already running thread %d", mPid );
}
mThreadCondition.wait();
mThreadMutex.unlock();
Debug( 1, "Started thread %d", mPid );
}
示例2: start
virtual void
start(Runnable *runnable, bool detached, bool runnableShouldBeFreed) throw(ThreadException) {
this->runnable = runnable;
this->detached = detached;
this->runnableShouldBeFreed = runnableShouldBeFreed;
if (detached) {
pthread_attr_t attr;
if (pthread_attr_init(&attr) != 0) {
throw ThreadException("Cannot initialize pthread attribute.");
}
if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
throw ThreadException("Cannot set pthread detach state.");
}
ref();
if (pthread_create(&thread, &attr, entry, this) != 0) {
unref();
throw ThreadException("Cannot create a thread.");
}
pthread_attr_destroy(&attr);
} else {
ref();
if (pthread_create(&thread, NULL, entry, this) != 0) {
unref();
throw ThreadException("Cannot create a thread.");
}
}
}
示例3: _threadbase
/*! Ctor.
\param detach detach thread if true
\param stacksize default thread stacksize */
_threadbase(const bool detach, const size_t stacksize) throw(ThreadException) : _attr(), _tid(), _exitval()
{
if (pthread_attr_init(&_attr))
throw ThreadException("pthread_attr_init failure");
if (stacksize && pthread_attr_setstacksize(&_attr, stacksize))
throw ThreadException("pthread_attr_setstacksize failure");
if (detach && pthread_attr_setdetachstate(&_attr, PTHREAD_CREATE_DETACHED))
throw ThreadException("pthread_attr_setdetachstate failure");
}
示例4: m_runnable
Thread::Thread(IRunnable& runnable, bool detach) :
m_runnable(runnable)
{
if (pthread_attr_init(&m_attr) != 0)
throw ThreadException("pthread_attr_init() error", __LINE__, __FILE__);
if (detach && pthread_attr_setdetachstate(&m_attr, PTHREAD_CREATE_DETACHED) != 0)
throw ThreadException("pthread_attr_setdetachstate error", __LINE__, __FILE__);
m_started = false;
}
示例5: switch
void
MutualExclusion::Unlock()
{
switch (pthread_mutex_unlock(&mutexTheMutex))
{
case 0: return;
case EPERM: throw ThreadException(ThreadException::errMutexNotOwned);
default: throw ThreadException(ThreadException::errMutexUnknown);
}
}
示例6: switch
/**
* Signal a event variable, waking up any waiting thread(s).
*/
void Event::notify()
{
int rc = 0;
mutex.lock();
switch (type) {
case DM1_EVENT_SINGLE:
if (waitersCount == 0) {
signaled = true;
}
else {
rc = pthread_cond_signal(&cond);
}
break;
case DM1_EVENT_BROADCAST:
rc = pthread_cond_broadcast(&cond);
break;
}
mutex.unlock();
if (rc != 0) {
fprintf(stderr, "Event.notify: Failed to notify Event: errcode = %d\n", rc);
throw ThreadException(__FILE__, __LINE__, DM1_ERR_EVENT_NOTIFY, rc);
}
}
示例7: while
// Sets a new prompt for the user and then gets an entire string from the input
// queue for this thread's node. If no string present, waits until one is.
char* GameThread::getStr(char* szBuffer, char* szPromptText, short nPromptCol, short nMaxLen)
{
// Display the new prompt
gn->print(szPromptText, nPromptCol, 0, OP_COMMAND_PROMPT);
gs->leaveCritical();
// While there's no input messages waiting, sleep.
while ( gn->mqInput.isEmpty() )
{
Sleep(50);
}
gs->enterCritical();
// Retreive the first waiting input message
InputData idMessage = gn->mqInput.dequeue();
// If it's an IP_FORCE_EXIT message, shut down this thread.
if ( idMessage.nType == IP_FORCE_EXIT )
throw ThreadException();
// If the message is too long, shorten it.
if ( strlen(idMessage.szMessage) > nMaxLen && nMaxLen < 190 )
idMessage.szMessage[nMaxLen] = '\0';
// Copy the message to the buffer and return it.
strcpy(szBuffer, idMessage.szMessage);
return szBuffer;
}
示例8: Warning
Mutex::~Mutex()
{
if ( locked() )
Warning( "Destroying mutex when locked" );
if ( pthread_mutex_destroy( &mMutex ) < 0 )
throw ThreadException( stringtf( "Unable to destroy pthread mutex: %s", strerror(errno) ) );
}
示例9: create
void create(U callObj, T fctParam) {
mCallObj = callObj;
mFctParam = fctParam;
if (pthread_create(&mThread, NULL, start_thread_trampoline<U, T>, this) != UnixThread::PTHREAD_SUCCESS)
throw ThreadException("fail pthread_create()");
}
示例10: run
virtual unsigned run() throw(ThreadException)
{
Worker::run();
throw ThreadException("test exception throwing.");
return 1;
}
示例11: ENTER
/**
* This is the method that should be called to start the Thread
*/
void
Thread::start()
{
ENTER("Thread.start");
if (debug)
fprintf(stdout, "Thread.start: Starting thread %s\n", getName());
#if DM1_USE_PTHREAD
int rc = pthread_create(&tid, 0, dm1_thread_main, this);
if (rc != 0) {
#else
handle = (HANDLE) _beginthreadex(NULL, 0, dm1_thread_main,
this, 0, (unsigned int *)&tid);
//handle = (HANDLE) CreateThread(NULL, 0, dm1_thread_main,
// this, CREATE_SUSPENDED, &tid);
//if (handle == 0 || ResumeThread(handle) == (DWORD)-1) {
if (handle == 0) {
int rc = GetLastError();
#endif
fprintf(stderr, "Thread.start: Error: Failed to start thread %s\n", getName());
throw ThreadException(__FILE__, __LINE__, DM1_ERR_THREAD_CREATE, rc);
}
EXIT("Thread.start");
}
/**
* The Thread constructor.
*/
Thread::Thread(Runnable *toRun, const char *name, bool isDaemon) : DLinkable()
{
ENTER("Thread.ctor");
status = DM1_THREAD_UNUSED;
this->isDaemon = isDaemon;
this->toRun = toRun;
toRun->setThread(this);
latchMode = 0;
isSignaled = 0;
this->name = strdup(name);
status = DM1_THREAD_INITED;
EXIT("Thread.ctor");
}
/**
* Terminates the thread.
*/
void
Thread::exit()
{
ENTER("Thread.exit");
if (debug)
fprintf(stdout, "Thread.exit: Exiting thread %s\n", getName());
#if !DM1_USE_PTHREAD
cleanup();
_endthreadex(0);
//ExitThread(0);
#else
pthread_exit(0);
#endif
EXIT("Thread.exit");
}
示例12: GetLastError
void Event::reset()
{
if (!ResetEvent(event)) {
DWORD rc = GetLastError();
fprintf(stderr, "Event.reset: Failed to reset an Event: errcode = %d\n", rc);
throw ThreadException(__FILE__, __LINE__, DM1_ERR_EVENT_RESET, rc);
}
}
示例13: strerror
void CommonThread::start() {
if(!this->_create){
if(pthread_create(&this->thread_info.thread_id, NULL, start_thread, (void*)this) != 0) {
stringstream error;
error << "[create] create thread failed:" << strerror(errno) << endl;
throw ThreadException(error.str());
}
if (this->thread_info.detach == E_THREAD_DETACHED) {
cout << "Detached Thread:" << this->thread_info.name << endl;
if (pthread_detach(this->thread_info.thread_id)) {
throw ThreadException("[detach] can't create detached thread");
}
}
this->_create = T_TRUE;
this->thread_info.state = E_THREAD_RUNNING;
}
}
示例14: getTimeout
bool Condition::wait( double secs )
{
// Locking done outside of this function
struct timespec timeout = getTimeout( secs );
if ( pthread_cond_timedwait( &mCondition, mMutex.getMutex(), &timeout ) < 0 && errno != ETIMEDOUT )
throw ThreadException( stringtf( "Unable to timedwait pthread condition: %s", strerror(errno) ) );
return( errno != ETIMEDOUT );
}
示例15: pthread_mutex_trylock
bool Mutex::locked()
{
int state = pthread_mutex_trylock( &mMutex );
if ( state != 0 && state != EBUSY )
throw ThreadException( stringtf( "Unable to trylock pthread mutex: %s", strerror(errno) ) );
if ( state != EBUSY )
unlock();
return( state == EBUSY );
}