Knight's Tour Generator
Tourneys and the Fast Generation and Obfuscation of Closed Knight's Tours
ThreadSafeQueue.cpp
Go to the documentation of this file.
1 
4 // MIT License
5 //
6 // Copyright (c) 2019 Ian Parberry
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 
26 #include "ThreadSafeQueue.h"
27 #include "Generator.h"
28 
31 
32 template <class data> void CThreadSafeQueue<data>::push(const data& element){
33  m_mutex.lock();
34  m_stdQueue.push(element);
35  m_mutex.unlock();
36 } //push
37 
41 
42 template <class data> bool CThreadSafeQueue<data>::pop(data& element){
43  bool success = false; //true if there was something to delete
44  m_mutex.lock();
45 
46  if(!m_stdQueue.empty()){ //queue has something in it
47  element = m_stdQueue.front(); //get element from front of queue
48  m_stdQueue.pop(); //delete from front of queue
49  success = true; //success
50  } //if
51 
52  m_mutex.unlock();
53  return success;
54 } //pop
55 
58 
59 template <class data> size_t CThreadSafeQueue<data>::size(){
60  return m_stdQueue.size();
61 } //size
62 
64 
65 //explicit template instantiations
66 
67 template class CThreadSafeQueue<CSearchRequest>;
68 template class CThreadSafeQueue<CSearchResult>; ///< Threadable queue of results.
Header for the thread safe queue CThreadSafeQueue.
void push(const data &element)
Insert at tail.
Header for the tourney and knight's tour generator CGenerator.
size_t size()
Get queue size.
bool pop(data &element)
Delete from head and return.