/** * Push an element onto the queue. If the queue has a max size, * this call will block if the queue is full. */ void push(T value) { constexpr const std::chrono::milliseconds max_wait{10}; #ifdef OSMIUM_DEBUG_QUEUE_SIZE ++m_push_counter; #endif if (m_max_size) { while (size() >= m_max_size) { std::unique_lock<std::mutex> lock{m_mutex}; m_space_available.wait_for(lock, max_wait, [this] { return m_queue.size() < m_max_size; }); #ifdef OSMIUM_DEBUG_QUEUE_SIZE ++m_full_counter; #endif } } std::lock_guard<std::mutex> lock{m_mutex}; m_queue.push(std::move(value)); #ifdef OSMIUM_DEBUG_QUEUE_SIZE if (m_largest_size < m_queue.size()) { m_largest_size = m_queue.size(); } #endif m_data_available.notify_one(); }
void consumeToLog() { while (true) { try { typename LogObject::SmartPtr logObj; { boost::mutex::scoped_lock lock(_qMutex); while (_queue.size() == 0) { if (_isStopping) { std::cout << " Stopping consumeToLog Thread. " << std::endl; return; } _qCondVar.wait(lock); } // Get the entry logObj = _queue.front(); _queue.pop(); } printLogObject(logObj); } catch (const boost::thread_interrupted& err) { std::cout << " Log::consumeToLog() - Got Interrupt Signal. " << _queue.size() << std::endl; } }//while }
bool Market::match( std::queue < Order > & orders ) { while ( true ) { if ( !m_bidOrders.size() || !m_askOrders.size() ) return orders.size() != 0; BidOrders::iterator iBid = m_bidOrders.begin(); AskOrders::iterator iAsk = m_askOrders.begin(); if ( iBid->second.getPrice() >= iAsk->second.getPrice() ) { Order & bid = iBid->second; Order& ask = iAsk->second; match( bid, ask ); orders.push( bid ); orders.push( ask ); if ( bid.isClosed() ) m_bidOrders.erase( iBid ); if ( ask.isClosed() ) m_askOrders.erase( iAsk ); } else return orders.size() != 0; } }
// Returns FALSE if message not read, TRUE if it was read. Will always return TRUE if "blocking" is set. // Will throw MDFN_Error if the read message code is CDIF_MSG_FATAL_ERROR bool CDIF_Queue::Read(CDIF_Message *message, bool blocking) { bool ret = true; slock_lock((slock_t*)ze_mutex); if(blocking) { while(ze_queue.size() == 0) // while, not just if. scond_wait((scond_t*)ze_cond, (slock_t*)ze_mutex); } if(ze_queue.size() == 0) ret = false; else { *message = ze_queue.front(); ze_queue.pop(); } slock_unlock((slock_t*)ze_mutex); if(ret && message->message == CDIF_MSG_FATAL_ERROR) throw MDFN_Error(0, "%s", message->str_message.c_str()); return(ret); }
void Queue (int size, Type sample, std::queue<Type> &queue) { // number of parameters use to get the mean value. // Fill the queue with n samples if (queue.size() < size) { //add a new element in the queue queue.push (sample); } if (queue.size() > size && !queue.empty()) { //add a new element in the queue while reduce its size by two till the the queue reach a smaller size //remove last element queue.pop (); //insert new element queue.push (sample); //remove last element queue.pop (); } if (queue.size() == size && !queue.empty()) { //remove last element queue.pop(); //insert new element queue.push (sample); } }
void useFrame(cv::Mat& mRgba){ int64 now = cv::getTickCount(); int64 then; time_queue.push(now); // Process frame if(mRgba.cols != 0) { processFrame(mRgba); char buffer[256]; sprintf(buffer, "Display performance: %dx%d @ %.3f", mRgba.cols, mRgba.rows, fps); cv::putText(mRgba, std::string(buffer), cv::Point(8,64), cv::FONT_HERSHEY_COMPLEX_SMALL, 1, cv::Scalar(0,255,255,255)); } if (time_queue.size() >= 2) then = time_queue.front(); else then = 0; if (time_queue.size() >= 25) time_queue.pop(); fps = time_queue.size() * (float)cv::getTickFrequency() / (now-then); }
//In morrowind this only got called for keyboards when typing into the console. Probably no point in overriding it //And so it turns out that oblivion uses this whever it's in menumode instead of just consoles. Figures. HRESULT _stdcall GetDeviceData(DWORD a,DIDEVICEOBJECTDATA* b,DWORD* c,DWORD d) { if (bufferedPresses.empty()) return RealDevice->GetDeviceData(a,b,c,d); if(!b) { DWORD temp=*c; HRESULT hr = RealDevice->GetDeviceData(a,b,c,d); if(c) *c=min(bufferedPresses.size(),temp); if(!(d|DIGDD_PEEK)) while(!bufferedPresses.empty()) bufferedPresses.pop(); return hr; } int count=0; while (bufferedPresses.size()) { //Stricktly speaking, should return a buffer overflow by here, but if you do it breaks? //Presumably, if you could mash your keyboard fast enough, no keypresses would register... if(count==*c) return DI_OK; //DI_BUFFEROVERFLOW; //This will not work correctly if DIGDD_PEEK is specified. afaik, it's only ever used if b == NULL *b=bufferedPresses.front(); bufferedPresses.pop(); b+=sizeof(void*); count++; } if(count==*c) return DI_OK; //Can probably just return DI_OK here, because afaik *c is only ever 1 unless oblivion is trying to empty the buffer *c-=count; HRESULT hr=RealDevice->GetDeviceData(a,b,c,d); *c+=count; return hr; }
int main(int argc, const char * argv[]) { scanf("%s%s", s1, s2); if(strlen(s1) != strlen(s2)){ puts("-1"); }else{ for(int i = 0;s1[i];++i){ if(s1[i] == '+') a.push(i); if(s2[i] == '+') b.push(i); } if(a.size() != b.size()){ //puts("diff size"); puts("-1"); }else{ int ans = 0; while(!a.empty()){ ans += abs(a.front() - b.front()); a.pop(); b.pop(); } printf("%d\n", ans); } } return 0; }
void* thread_save_image(void*) { timespec time_save0, time_save1, t_sleep, t_rem; t_sleep.tv_sec = 0; t_sleep.tv_nsec = 10; for (;;) { // Wait for image capture if (save_frame_buffer.size() > 0) { clock_gettime( CLOCK_REALTIME, &time_save0); write_jpeg(save_frame_buffer.front()); // write_png(save_frame_buffer.front()); if (save_frame_buffer.size() > 4) { int bufsize = save_frame_buffer.front().width * save_frame_buffer.front().height * save_frame_buffer.size() / 1024.0; std::cout << "\rBuffer queue: " << bufsize << " kB " << std::flush; } std::cout << "d" << std::flush; pthread_mutex_lock( &save_buffer_mutex ); save_frame_buffer.pop(); pthread_mutex_unlock( &save_buffer_mutex ); clock_gettime(CLOCK_REALTIME, &time_save1); double twrite = tdiff(time_save1, time_save0); } else { nanosleep(&t_sleep, &t_rem); } } }
// Returns false if message not read, true if it was read. Will always return true if "blocking" is set. // Will throw MDFN_Error if the read message code is CDIF_MSG_FATAL_ERROR bool CDIF_Queue::Read(CDIF_Message *message, bool blocking) { bool ret = true; // // // MDFND_LockMutex(ze_mutex); if(blocking) { while(ze_queue.size() == 0) // while, not just if. { MDFND_WaitCond(ze_cond, ze_mutex); } } if(ze_queue.size() == 0) ret = false; else { *message = ze_queue.front(); ze_queue.pop(); } MDFND_UnlockMutex(ze_mutex); // // // //if(ret && message->message == CDIF_MSG_FATAL_ERROR) // throw MDFN_Error(0, "%s", message->str_message.c_str()); return(ret); }
/** print queues */ void print(){ std::cout << "\n" << std::endl; int i; for(i=0; i<master.size(); ++i){ struct Qu t = master.front(); std::cout << "master: " << t.proc_num << std::endl; master.pop(); master.push(t); } for(i=0; i<ready.size(); ++i){ struct Qu t = ready.front(); std::cout << "ready: " << t.proc_num << std::endl; ready.pop(); ready.push(t); } for(i=0; i<waiting.size(); ++i){ struct Qu t = waiting.front(); std::cout << "waiting: " << t.proc_num << std::endl; waiting.pop(); waiting.push(t); } for(i=0; i<completed.size(); ++i){ struct Qu t = completed.front(); std::cout << "completed: " << t.proc_num << std::endl; completed.pop(); completed.push(t); } for(i=0; i<priority.size(); ++i){ struct Qu t = priority.front(); std::cout << "priority " << t.proc_num << std::endl; priority.pop_front(); priority.push_back(t); } }
void check() { BOOST_REQUIRE(queue); BOOST_CHECK_EQUAL((u_int32_t) ids.size(), queue->getMessageCount()); while (pop()) ;//keeping popping 'till all messages are dequeued BOOST_CHECK_EQUAL((u_int32_t) 0, queue->getMessageCount()); BOOST_CHECK_EQUAL((size_t) 0, ids.size()); }
void TaskEngine::waitThreads() { if (tasks.size() > 0) { SDL_LockMutex(threadLock); while (tasks.size() != 0) { SDL_CondWait(taskAvailable, threadLock); } SDL_UnlockMutex(threadLock); } }
UINT CALLBACK LogThread(void* param) { TCHAR fileName[MAX_PATH]; LogPath(fileName, _T("log")); while ( m_bLoggerRunning || (m_logQueue.size() > 0) ) { if ( m_logQueue.size() > 0 ) { SYSTEMTIME systemTime; GetLocalTime(&systemTime); WIN32_FILE_ATTRIBUTE_DATA fileInformation; GetFileAttributesEx(fileName, GetFileExInfoStandard, &fileInformation); if(logFileParsed != systemTime.wDay || fileInformation.nFileSizeLow > 10485760) { LogRotate(); logFileParsed=systemTime.wDay; LogPath(fileName, _T("log")); } CAutoLock lock(&m_logFileLock); FILE* fp = _tfopen(fileName, _T("a+")); if (fp!=NULL) { SYSTEMTIME systemTime; GetLocalTime(&systemTime); wstring line = GetLogLine(); while (!line.empty()) { fwprintf_s(fp, L"%s", line.c_str()); line = GetLogLine(); } fclose(fp); } else //discard data { wstring line = GetLogLine(); while (!line.empty()) { line = GetLogLine(); } } } if (m_bLoggerRunning) { m_EndLoggingEvent.Wait(1000); //Sleep for 1000ms, unless thread is ending } else { Sleep(1); } } _endthreadex(0); return 0; }
void popAll() { std::vector<Invocation> others; mutex.Lock(); others.reserve(q.size()); for (int i = 0, n = q.size(); i < n; i++) { others.push_back(q.front()); q.pop(); } mutex.Unlock(); for (Invocation& invok : others) { invok.invoke(); } }
void merge_thread(void *arg) { merge_arg_t a, b, c; bool cont, quit; while (true) { cont = false; quit = false; size_t waiting = 0, remaining = 0; while (true) { pthread_mutex_lock(&parts_mutex); waiting = pending_parts.size(); remaining = remaining_parts; pthread_mutex_unlock(&parts_mutex); if (waiting >= 2) { pthread_mutex_lock(&parts_mutex); a = pending_parts.front(); pending_parts.pop(); b = pending_parts.front(); pending_parts.pop(); remaining_parts -= 1; pthread_mutex_unlock(&parts_mutex); break; } else if (waiting + remaining >= 2) { if (pthread_mutex_trylock(&parts_exist_mutex) != 0) { return; } } else { return; } } c.data = merge_sorted(a, b); c.begin = 0; c.end = c.data->size(); c.delete_data_when_done = true; pthread_mutex_lock(&parts_mutex); pending_parts.push(c); if (pending_parts.size() <= 2) { pthread_mutex_trylock(&parts_exist_mutex); pthread_mutex_unlock(&parts_exist_mutex); } pthread_mutex_unlock(&parts_mutex); } }
void pushRequest(Request& r) { if (reqs.size() + 1 > networkRequestMax) { //超过请求队列长度,试图精简队列 if (!reqs.front().isImportant()) reqs.pop(); } if (reqs.size() + 1 > networkRequestMax * 2) { //大量超过请求队列长度,只保留重要请求 std::queue<Request> q; while (reqs.size() != 0) { if (reqs.front().isImportant()) q.push(reqs.front()); reqs.pop(); } reqs = q; } reqs.push(r); }
/* Dispatch events to global listeners */ void eventsys::update( void ) { /* Add any pending listeners */ while( mNewEventListeners.size() > 0 ) { mEventListeners.push_back( mNewEventListeners.front() ); mNewEventListeners.pop(); } /* loop through events and loop through listeners, dispatch events to listeners*/ while( mEventList.size() > 0 ) { Event* e = mEventList.front(); for( auto i = mEventListeners.begin(); i != mEventListeners.end(); i++ ) { std::shared_ptr<EventListener> listener = (*i).lock(); if( listener ) { // Don't dispatch event back to it dispatcher if( listener.get() != e->sentBy ) listener->handle(e); } } mEventList.pop(); eventsys::release(e); } /* All done - clean up time. Remove deleted listeners */ while( mRemovedListeners.size() > 0) { std::weak_ptr<EventListener> del = mRemovedListeners.back(); mEventListeners.remove_if( [del](std::weak_ptr<EventListener> p ) { std::shared_ptr<EventListener> swp = del.lock(); std::shared_ptr<EventListener> sp = p.lock(); if( swp && sp ) return swp == sp; return false; }); // Don't quite understand lambda syntax. Stolen from here: http://stackoverflow.com/questions/10120623/removing-item-from-list-of-weak-ptrs mRemovedListeners.pop_back(); } }
static float getFps() { static std::queue<int64> time_queue; int64 now = cv::getTickCount(), then = 0; time_queue.push(now); if (time_queue.size() >= 2) then = time_queue.front(); if (time_queue.size() >= 25) time_queue.pop(); return time_queue.size() * (float)cv::getTickFrequency() / (now - then); }
void ferryCar(int time, int n, char* side, char* ferry_side,std::queue<int>& ferry) { if (!strcmp(side, ferry_side)) { if (ferry.size() < n) { } } else { if (ferry.size() < n) { } } }
// Returns FALSE if message not read, TRUE if it was read. Will always return TRUE if "blocking" is set. bool CDIF_Queue::Read(CDIF_Message *message, bool blocking) { if(!blocking && ze_queue.size() == 0) { return FALSE; } MDFND_WaitSemaphore(ze_semaphore); MDFND_LockMutex(ze_mutex); assert(ze_queue.size() > 0); *message = ze_queue.front(); ze_queue.pop(); MDFND_UnlockMutex(ze_mutex); return TRUE; }
BOOL __stdcall Hook_ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped) { if (hFile != hConnection) { return __ReadFile(hFile, lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead, lpOverlapped); } #if DEBUG_API logmsg("ReadFile(%x, %p, %d, %p, %p)\n", hFile, lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead, lpOverlapped); #endif if (replyBuffer.size()) { if (nNumberOfBytesToRead >= replyBuffer.size()) nNumberOfBytesToRead = replyBuffer.size(); *lpNumberOfBytesRead = nNumberOfBytesToRead; BYTE *ptr = (BYTE*) lpBuffer; for (DWORD i=0; i < nNumberOfBytesToRead; i++) { if (!replyBuffer.empty()) { *ptr++ = replyBuffer.front(); replyBuffer.pop(); } else { *lpNumberOfBytesRead = i; break; } } #if LOG_IO_STREAM // logmsg("Lidos %d\n", nNumberOfBytesToRead); ptr = (BYTE*) lpBuffer; logmsg("SD: "); for (DWORD i=0; i < nNumberOfBytesToRead; i++) { logmsg_nt("%02X ", (DWORD) *ptr++); } logmsg_nt("\n"); #endif } else { *lpNumberOfBytesRead = 0; return TRUE; } return TRUE; }
virtual uavcan::int16_t receive(uavcan::CanFrame& out_frame, uavcan::MonotonicTime& out_ts_monotonic, uavcan::UtcTime& out_ts_utc, uavcan::CanIOFlags& out_flags) { assert(this); out_flags = uavcan::CanIOFlags(); if (loopback.empty()) { EXPECT_TRUE(rx.size()); // Shall never be called when not readable if (rx_failure) { return -1; } if (rx.empty()) { return 0; } const FrameWithTime frame = rx.front(); rx.pop(); out_frame = frame.frame; out_ts_monotonic = frame.time; } else { out_flags |= uavcan::CanIOFlagLoopback; const FrameWithTime frame = loopback.front(); loopback.pop(); out_frame = frame.frame; out_ts_monotonic = frame.time; } out_ts_utc = enable_utc_timestamping ? iclock.getUtc() : uavcan::UtcTime(); return 1; }
void saveQueue(std::queue<vec> s, int vecSize, char filename[]) { int sizeQueue=s.size(); int tmpSize=vecSize*sizeQueue; double tmp[tmpSize]; int count=0; while(sizeQueue!=0){ itpp::vec x= s.front(); s.pop(); for(int i=0;i<vecSize;i++){ //DispVal(count); tmp[count]=x[i]; count=count+1; } sizeQueue--; } std::ofstream ofs( filename , std::ifstream::out ); ofs.write((char * ) tmp, tmpSize*sizeof(double)); ofs.close(); return; }
void messageDelay() { time_t t1 = time(NULL); if (message_queue.size() > 0 && t1 >= delay_time) { std::string message = message_queue.front(); int clientID = message[0] - '0'; int index = 0; if (message.find("COMMAND") != std::string::npos) { index = message.find("COMMAND"); InterpretCommand(clientID, message.substr(index)); } else if (message.find("NewPlayer") != std::string::npos){ index = message.find("NewPlayer"); InterpretCommand(clientID, message.substr(index)); } time_t t2 = time(NULL); stringstream ss; ss << "t0:" << message.substr(4, message.length() - (message.length() - index + 2)) << ";t1:" << t1 << ";t2:" << t2; server.wsSend(clientID, ss.str()); delay_time = time(NULL) + (rand() % 10); message_queue.pop(); } }
extern "C" void Java_org_ppsspp_ppsspp_NativeApp_shutdown(JNIEnv *, jclass) { if (useCPUThread && graphicsContext) { EmuThreadStop(); while (emuThreadState != (int)EmuThreadState::STOPPED) { graphicsContext->ThreadFrame(); } EmuThreadJoin(); } ILOG("NativeApp.shutdown() -- begin"); if (renderer_inited) { ILOG("Shutting down renderer"); // This will be from the wrong thread? :/ graphicsContext->Shutdown(); delete graphicsContext; graphicsContext = nullptr; renderer_inited = false; } else { ILOG("Not shutting down renderer - not initialized"); } NativeShutdown(); VFSShutdown(); while (frameCommands.size()) frameCommands.pop(); ILOG("NativeApp.shutdown() -- end"); }
// Queues the current buffer, and gets a new write buffer void sound_xaudio2::roll_buffer() { // Don't queue a buffer if it is empty if (m_writepos == 0) return; // Queue the current buffer xaudio2_buffer buf; buf.AudioData = std::move(m_buffer); buf.AudioSize = m_writepos; m_queue.push(std::move(buf)); // Get a new buffer m_buffer = std::unique_ptr<BYTE[]>(m_buffer_pool->next()); m_writepos = 0; // We only want to keep a maximum number of buffers at any given time // so remove any from queue greater than MAX_QUEUED_BUFFERS if (m_queue.size() > m_buffer_count) { xaudio2_buffer *next_buffer = &m_queue.front(); // return the oldest buffer to the pool, and remove it from queue m_buffer_pool->return_to_pool(next_buffer->AudioData.release()); m_queue.pop(); m_overflows++; } }
void VDAgent::handle_control_event() { MUTEX_LOCK(_control_mutex); while (_control_queue.size()) { int control_command = _control_queue.front(); _control_queue.pop(); vd_printf("Control command %d", control_command); switch (control_command) { case CONTROL_STOP: _running = false; break; case CONTROL_DESKTOP_SWITCH: _desktop_switch = true; break; case CONTROL_LOGON: vd_printf("session logon"); // loading the display settings for the current session's logged on user only // after 1) we receive logon event, and 2) the desktop switched from Winlogon if (!_logon_desktop) { vd_printf("LOGON display setting"); _display_setting.load(); } else { _logon_occured = true; } break; case CONTROL_CLIPBOARD: _clipboard_tick = 0; break; default: vd_printf("Unsupported control command %u", control_command); } } MUTEX_UNLOCK(_control_mutex); }
// Returns FALSE if message not read, TRUE if it was read. Will always return TRUE if "blocking" is set. // Will throw MDFN_Error if the read message code is CDIF_MSG_FATAL_ERROR bool CDIF_Queue::Read(CDIF_Message *message, bool blocking) { TryAgain: MDFND_LockMutex(ze_mutex); if(ze_queue.size() > 0) { *message = ze_queue.front(); ze_queue.pop(); MDFND_UnlockMutex(ze_mutex); if(message->message == CDIF_MSG_FATAL_ERROR) throw MDFN_Error(0, "%s", message->str_message.c_str()); return(TRUE); } else if(blocking) { MDFND_UnlockMutex(ze_mutex); MDFND_Sleep(1); goto TryAgain; } else { MDFND_UnlockMutex(ze_mutex); return(FALSE); } }
void adapter_wait_for_event() { lock_q(); if (q_event.size()==0) pthread_cond_wait(&q_cond, &q_mutex); unlock_q(); }