Cuberite
A lightweight, fast and extensible game server for Minecraft
NetworkLookup.cpp
Go to the documentation of this file.
1 
2 // NetworkLookup.cpp
3 
4 // Implements the cNetworkLookup class representing an executor for asynchronous lookup tasks
5 
6 
7 #include "Globals.h"
8 #include "NetworkLookup.h"
9 
10 
11 
12 
14  cIsThread("Network Lookup Executor")
15 {
16 }
17 
18 
19 
20 
21 
23 {
24  Stop();
25 }
26 
27 
28 
29 
30 
31 void cNetworkLookup::ScheduleLookup(std::function<void()> a_Lookup)
32 {
33  m_WorkQueue.EnqueueItem(std::move(a_Lookup));
34 }
35 
36 
37 
38 
39 
41 {
42  m_ShouldTerminate = true;
44  m_WorkQueue.EnqueueItem([](){}); // Dummy work to wake up the thread
46 }
47 
48 
49 
50 
51 
53 {
54  while (!m_ShouldTerminate)
55  {
56  // Execute the next task in the queue
57  auto Work = m_WorkQueue.DequeueItem();
58  Work();
59  }
60 }
61 
62 
63 
std::atomic< bool > m_ShouldTerminate
The overriden Execute() method should check this value periodically and terminate if this is true.
Definition: IsThread.h:45
void Stop(void)
Signals the thread to terminate and waits until it's finished.
Definition: IsThread.cpp:48
virtual ~cNetworkLookup() override
virtual void Execute() override final
Process the queue until the thread is stopped.
void ScheduleLookup(std::function< void()> a_Lookup)
Schedule a lookup task for execution.
void Stop()
Cancels any scheduled lookups and joins the lookup thread.
cQueue< std::function< void()> > m_WorkQueue
The queue of lookup tasks waiting to be executed.
Definition: NetworkLookup.h:39
void EnqueueItem(ItemType a_Item)
Enqueues an item to the queue, may block if other threads are accessing the queue.
Definition: Queue.h:57
void Clear(void)
Removes all Items from the Queue, calling Delete on each of them.
Definition: Queue.h:128
ItemType DequeueItem(void)
Dequeues an item from the queue, blocking until an item is available.
Definition: Queue.h:100