mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 18:20:04 +00:00 
			
		
		
		
	Add a maximum number of forward/backward time steps that can be queued
This commit is contained in:
		
							parent
							
								
									3537a233ca
								
							
						
					
					
						commit
						97693fdace
					
				
					 4 changed files with 133 additions and 4 deletions
				
			
		
							
								
								
									
										49
									
								
								scwx-qt/source/scwx/qt/util/queue_counter.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								scwx-qt/source/scwx/qt/util/queue_counter.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,49 @@ | |||
| #include <scwx/qt/util/queue_counter.hpp> | ||||
| 
 | ||||
| #include <atomic> | ||||
| 
 | ||||
| namespace scwx::qt::util | ||||
| { | ||||
| 
 | ||||
| class QueueCounter::Impl | ||||
| { | ||||
| public: | ||||
|    explicit Impl(size_t maxCount) : maxCount_ {maxCount} {} | ||||
| 
 | ||||
|    const size_t        maxCount_; | ||||
|    std::atomic<size_t> count_ {0}; | ||||
| }; | ||||
| 
 | ||||
| QueueCounter::QueueCounter(size_t maxCount) : | ||||
|     p {std::make_unique<Impl>(maxCount)} | ||||
| { | ||||
| } | ||||
| 
 | ||||
| QueueCounter::~QueueCounter() = default; | ||||
| 
 | ||||
| bool QueueCounter::add() | ||||
| { | ||||
|    const size_t count = p->count_.fetch_add(1); | ||||
|    // Must be >= (not ==) to avoid race conditions
 | ||||
|    if (count >= p->maxCount_) | ||||
|    { | ||||
|       p->count_.fetch_sub(1); | ||||
|       return false; | ||||
|    } | ||||
|    else | ||||
|    { | ||||
|       return true; | ||||
|    } | ||||
| } | ||||
| 
 | ||||
| void QueueCounter::remove() | ||||
| { | ||||
|    p->count_.fetch_sub(1); | ||||
| } | ||||
| 
 | ||||
| bool QueueCounter::is_lock_free() | ||||
| { | ||||
|    return p->count_.is_lock_free(); | ||||
| } | ||||
| 
 | ||||
| } // namespace scwx::qt::util
 | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 AdenKoperczak
						AdenKoperczak