bool start_message_loop() { if(thread.get_id() != std::thread::id()) return false; thread = std::thread([this](){ if(::CoInitialize(nullptr)) return; ::quote::win32::message_loop([this](){ if(queue.empty()){ ::Sleep(1); return; } std::shared_ptr<async_message> message; if(queue.try_pop(message)){ switch(message->message){ case async_message::message_type::create: { auto param = reinterpret_cast<creation_params*>(message->data.get()); HWND hwnd = ::CreateWindowExW( param->exstyle, param->classname, param->title, param->style, param->x, param->y, param->w, param->h, param->hparent, nullptr, ::GetModuleHandleW(nullptr), param->data); message->result = hwnd != nullptr; std::lock_guard<std::mutex> lock(message->mutex); message->cv.notify_one(); } break; case async_message::message_type::destroy: { message->result = ::DestroyWindow(reinterpret_cast<HWND>(message->data.get())) != 0; std::lock_guard<std::mutex> lock(message->mutex); message->cv.notify_one(); } break; } } }); ::CoUninitialize(); }); return true; }
bool create(creation_params param) { if(param.w != CW_USEDEFAULT){ RECT rc = {0, 0, param.w, param.h}; ::AdjustWindowRectEx(&rc, param.style, FALSE, param.exstyle); param.w = rc.right - rc.left; param.h = rc.bottom - rc.top; } if(param.data == nullptr){ param.data = static_cast<Derived*>(this); } start_message_loop(); auto message = std::make_shared<async_message>(); std::unique_lock<std::mutex> lock(message->mutex); message->message = async_message::message_type::create; message->data = std::make_shared<creation_params>(param); queue.push(message); message->cv.wait_for(lock, std::chrono::seconds(3)); return message->result; }
void KinectAudioEventNotifier::ProcessEvents() { bool processing = true; AudioData* currentData = NULL; while (processing) { processing = speechDataToProcess.try_pop(currentData); if(processing) { for (int j = 0; j < listeners.size(); j++) { if(listeners[j]->Enabled) listeners[j]->AudioDataReceived(currentData); } if(currentData) { delete currentData; currentData = NULL; } } } }
void destroy() { // 同じスレッドからじゃないとDestroyWindowできない auto message = std::make_shared<async_message>(); std::unique_lock<std::mutex> lock(message->mutex); message->message = async_message::message_type::destroy; message->data = std::shared_ptr<void>(static_cast<Derived*>(this)->get_hwnd(), [](HWND){}); queue.push(message); message->cv.wait_for(lock, std::chrono::seconds(3)); }
bool test_ppl_parallel_for() { std::vector<int> a1 = { 1,2,3,4,5 }; std::vector<int> a2; std::vector<int> a3 = { 1,2,3,4,5 }; for (int i = 0; i < 32; ++i) { a2.push_back(i); } Concurrency::concurrent_queue<std::vector<int> > q; q.push(a1); q.push(a2); q.push(a3); Concurrency::parallel_for_each( q.unsafe_begin(), q.unsafe_end(), [&q](std::vector<int>& a) { for (auto x : a) { log_info "tid=%u, %u", GetCurrentThreadId(), x log_end; } log_info "tid=%u, done", GetCurrentThreadId() log_end; }); return true; }
void KinectAudioEventNotifier::InjectAudioFrameData(AudioData* audioData) { speechDataToProcess.push(audioData); }
LRESULT BTCtcp::ProcessReadMessage(HWND hWnd, LPARAM lParam) { if (msgIn.iMsgStatus != btcmsgstat_all) return -2; // can't process message, as it is not complete bool bFlag=false; switch (msgIn.iCommand) { case btcmsg_version: { // received a version msg --> send verack message back unsigned int iRcvdVer; std::vector<unsigned char>::iterator it=msgIn.vPayload.begin(); memcpy(&iRcvdVer, &it[0], sizeof(int)); // get version if (iRcvdVer>=209) { // node has sent best height information it +=80; // skip fixed length message data uint64 uil=msgIn.GetVarInt(it); // now a varlength string, uil=its length it +=uil; memcpy(&Peer_BlockHeight,&it[0],sizeof(int)); } else { mysqlpp::Connection * c = mydb.GrabConnection(); Peer_BlockHeight = mydb.GetBestHeightKnown(c); mydb.ReleaseConnection(c); } bFlag = SendMsg_Verack(); } break; case btcmsg_verack: // received a verack msg --> check if this was indeed in response to our version command if (iBTCNodeStatus==1) { // sent indeed a version command iBTCNodeStatus++; // go to next lvl :) PostMessage(hWnd, myMsg_ProcessNodeStatus, 0, MsgREPAINT); // change in node status, do next things } bFlag=true; // consider it always processed break; case btcmsg_inv: // received a inv msg --> check if it was asked for, or unsollicited if (iBTCNodeStatus>=3) { BTCMessage *btcmsgcopy = new BTCMessage(msgIn); // copy the message cqInvMsg.push(*btcmsgcopy); // push it on queue for worker thread } if (iBTCNodeStatus==3) { // we asked for blocks, here is our response probably iBTCNodeStatus++; // go to next lvl :) PostMessage(hWnd, myMsg_ProcessNodeStatus, 0, MsgREPAINT); } bFlag=true; break; case btcmsg_block: // receiving block data if (iBTCNodeStatus>3) { // we accept "block" messages BTCMessage *btcmsgcopy = new BTCMessage(msgIn); // copy the message cqBlockMsg.push(*btcmsgcopy); // push it on queue for worker thread } bFlag=true; break; case btcmsg_tx: // receiving a transaction if (iBTCNodeStatus>4) { // we accept tx messages BTCMessage *btcmsgcopy = new BTCMessage(msgIn); // copy the message cqTxMsg.push(*btcmsgcopy); // push it on stack for worker thread } bFlag=true; break; default: // digest unknown messages bFlag=true; } if (bFlag) { // everything went well in switch --> msgIn is processed msgIn.Init(BTC_TestNetwork); // reinitilize it PostMessage(hWnd, myMsg_ProcessReadBuffer, 1, 0); // see if any other message is already waiting in Buffer ! } return (bFlag? 0 : -1); }