本文整理汇总了C++中Runnable类的典型用法代码示例。如果您正苦于以下问题:C++ Runnable类的具体用法?C++ Runnable怎么用?C++ Runnable使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Runnable类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: addProcessor
bool Worker::addProcessor(int id, Runnable *processor)
{
bool ret = false;
std::map<int, Runnable*> runnables;
Runnable* current;
std::lock_guard<std::mutex> guard(mtx);
while (!processors.empty()){
current = processors.top();
processors.pop();
runnables[current->getId()] = current;
}
if (runnables.count(id) == 0) {
processor->setId(id);
runnables[id] = processor;
ret = true;
}
for (auto it : runnables){
processors.push(it.second);
}
return ret;
}
示例2: removeProcessor
bool Worker::removeProcessor(int id)
{
bool ret = false;
std::map<int, Runnable*> runnables;
Runnable* current;
std::lock_guard<std::mutex> guard(mtx);
while (!processors.empty()){
current = processors.top();
processors.pop();
runnables[current->getId()] = current;
}
while (runnables.count(id) > 0) {
runnables.erase(id);
ret = true;
}
for (auto it : runnables){
processors.push(it.second);
}
return ret;
}
示例3: execute_thread
static void* execute_thread(void * ptr)
{
Runnable* runnable = static_cast<Runnable*>(ptr);
assert(runnable);
runnable->run();
pthread_exit(NULL);
}
示例4: while
void AsyncTaskExecutor::run()
{
while(_isRunning)
{
std::unique_lock<std::mutex> lock(_mutex);
if(!_queue.empty())
{
Runnable *task = _queue.front();
_queue.pop_front();
if(task!=NULL)
{
_isBusy = true;
_currentTask = task;
lock.unlock();
task->run();
lock.lock();
_currentTask = NULL;
if(task->toDelete())
delete task;
}
}
else if(_isRunning)
{
_isBusy = false;
_signal.wait(lock);
}
}
return;
}
示例5: getState
//TODO: avoid void functions
void Worker::getState(Jzon::Object &workerNode)
{
Jzon::Array pList;
std::map<int, Runnable*> runnables;
Runnable* current;
{
std::lock_guard<std::mutex> guard(mtx);
while (!processors.empty()){
current = processors.top();
processors.pop();
runnables[current->getId()] = current;
}
for (auto it : runnables){
processors.push(it.second);
}
}
for (auto it : runnables) {
pList.Add(it.first);
}
workerNode.Add("type", utils::getWorkerTypeAsString(type));
workerNode.Add("processors", pList);
}
示例6: lock
Milliseconds RunLoop::Impl::processRunnables() {
std::lock_guard<std::recursive_mutex> lock(mtx);
auto now = Clock::now();
auto nextDue = TimePoint::max();
// O(N) but in the render thread where we get tons
// of messages, the size of the list is usually 1~2.
for (nextRunnable = runnables.begin(); nextRunnable != runnables.end();) {
Runnable* runnable = *(nextRunnable++);
auto const dueTime = runnable->dueTime();
if (dueTime <= now) {
runnable->runTask();
} else {
nextDue = std::min(nextDue, dueTime);
}
}
if (runnables.empty() || nextDue == TimePoint::max()) {
return Milliseconds(-1);
}
auto timeout = std::chrono::duration_cast<Milliseconds>(nextDue - now);
if (alarm) {
alarm->invoke(&Alarm::set, timeout);
}
return timeout;
}
示例7:
void
RunnableHandler::handleEvent( void * e)
{
Runnable *r = reinterpret_cast< Runnable * > (e);
r->run();
if( d_->del_ )
{
delete r;
}
}
示例8: pthread_setcanceltype
void *Thread_Unix::thread_main(void *data)
{
// kill thread immediately - no cancellation point
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, nullptr);
Runnable *runnable = (Runnable *)data;
ThreadLocalStorage tls;
runnable->run();
return nullptr;
}
示例9: lock
void TaskExecutor::clearAll()
{
std::lock_guard<std::mutex> lock(_mutex);
for(int i = 0; i < _queue.size(); i++)
{
Runnable *task = _queue[i];
if(task && task->toDelete())
delete task;
}
_queue.clear();
}
示例10: handleMessage
virtual void handleMessage(const Message& message) {
switch (message.what) {
case 4:
case 5: {
Runnable* runnable = (Runnable*) message.obj;
runnable->run();
break;
}
default:
Log::i(LOG_TAG, "Handler1::handleMessage 0x%x with ID %d by Looper 0x%x", &message, message.what, Looper::myLooper());
}
}
示例11: Runnable
void test_suite::func_begin(const std::string & name)
{
// Run test initializers
const_it it = test_vec.find(name);
if(it!=test_vec.end())
{
(*it).second->begin();
return;
}
Runnable *pTest = new Runnable(name);
test_vec.insert(pair<string,Runnable*>(name,pTest));
none_init_vec.push_back(pTest);
pTest->begin();
}
示例12: onStarted
void ThreadQueue::onStarted()
{
Runnable* r;
// qDebug() << "has started";
while ((r = hasMore()) != NULL)
{
//qDebug() << "executing next from list" << r->name();
QVariant result = r->run();
emit finished(r, result);
}
thread()->quit();
}
示例13:
/**
执行任务的工作线程。
当前没有任务时,
如果当前线程数量大于最小线程数量,减少线程,
否则,执行清理程序,将线程类给释放掉
**/
void CThreadPoolExecutor::CWorker::Run()
{
Runnable * pTask = NULL;
while (m_bRun)
{
if (NULL == m_pFirstTask)
{
pTask = m_pThreadPool->GetTask();
}
else
{
pTask = m_pFirstTask;
m_pFirstTask = NULL;
}
if (NULL == pTask)
{
EnterCriticalSection(&(m_pThreadPool->m_csThreadPoolLock));
if (m_pThreadPool->GetThreadPoolSize() > m_pThreadPool->m_minThreads)
{
ThreadPoolItr itr = m_pThreadPool->m_ThreadPool.find(this);
if (itr != m_pThreadPool->m_ThreadPool.end())
{
m_pThreadPool->m_ThreadPool.erase(itr);
m_pThreadPool->m_TrashThread.insert(this);
}
m_bRun = false;
}
else
{
ThreadPoolItr itr = m_pThreadPool->m_TrashThread.begin();
while (itr != m_pThreadPool->m_TrashThread.end())
{
(*itr)->Join();
delete (*itr);
m_pThreadPool->m_TrashThread.erase(itr);
itr = m_pThreadPool->m_TrashThread.begin();
}
}
LeaveCriticalSection(&(m_pThreadPool->m_csThreadPoolLock));
continue;
}
else
{
pTask->Run();
pTask = NULL;
}
}
}
示例14: switch
void ChannelService::OnSoftSignal(uint32 soft_signo, uint32 appendinfo)
{
switch (soft_signo)
{
case CHANNEL_REMOVE:
{
//uint32 chanel_id = appendinfo;
VerifyRemoveQueue();
break;
}
case WAKEUP:
{
Runnable* task = NULL;
while (m_pending_tasks.Pop(task))
{
if (NULL != task)
{
task->Run();
}
}
break;
}
case USER_DEFINED:
{
if (NULL != m_user_cb)
{
m_user_cb(this, appendinfo, m_user_cb_data);
}
break;
}
case CHANNEL_ASNC_IO:
{
ChannelAsyncIOContext ctx;
while (m_async_io_queue.Pop(ctx))
{
if (NULL != ctx.cb)
{
Channel* ch = GetChannel(ctx.channel_id);
ctx.cb(ch, ctx.data);
}
}
break;
}
default:
{
break;
}
}
}
示例15: OnTaskHandleMsg
LRESULT OnTaskHandleMsg(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
Runnable* pRunnable = (Runnable *)wParam;
if (pRunnable)
{
pRunnable->Run();
if (pRunnable->IsBeHosted())
{
pRunnable->Release();
}
}
return 0;
}