TestsTested | ✓ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Jul 2016 |
Maintained by Shaheen Ghiassy.
To run the example project, clone the repo, and run pod install
from the Example directory first.
thread-safe-block-queue is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "thread-safe-block-queue"
Install the Pod
pod install
Then add to your code
#import <thread-safe-block-queue/ThreadSafeBlockQueue.h>
ThreadSafeBlockQueue *queue = [[ThreadSafeBlockQueue alloc] init];
Thread Safe Block Queue (TSBQ) is a special data structure I needed once. Much like NSOperation, it provides a FIFO queue for running blocks. The queue is initially suspended and later is resumed by the developer.
What TSBQ offers beyond an NSOperationQueue is ability to replay the blocks given to TSBQ. This unique feature was necessary in a project I was working on and I'm making it available publicly since its highly tested and thread-safe.
// ┌────────────────────────────────┐
// │ Queue starts at HEAD with 0 │
// └────────────────────────────────┘
// │
// │
// │
// │
// │
// │
// ▼
//
// null
//
//
//
//
//
//
// ┌─────────────────────────────────────────────┐
// │ Blocks come in and are queued FIFO │
// └─────────────────────────────────────────────┘
// │
// │
// │
// │
// │
// │
// ▼
// ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐
// │ │ │ │ │ │ │ │ │ │
// │ Block │ │ Block │ │ Block │ │ Block │ │ Block │
// │ │ │ │ │ │ │ │ │ │
// └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘
//
//
//
//
//
//
//
//
// ┌───────────────────────────────────────────────────────────────────────────────┐
// │ Dequeuing the data structure causes the blocks to be run in sequential order │
// └───────────────────────────────────────────────────────────────────────────────┘
// │
// │
// │
// ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ▶ │
// │
// │
// ▼ ▼
// ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐
// │ │ │ │ │ │ │ │ │ │
// │ Block │ │ Block │ │ Block │ │ Block │ │ Block │
// │ │ │ │ │ │ │ │ │ │
// └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘
//
//
//
//
//
//
//
//
//
//
// ┌──────────────────────────────────────────┐
// │Subsequent blocks will be run immediately │
// └──────────────────────────────────────────┘
// │
// │
// ─ ─ ─ ─ ─▶ │
// │
// │
// │
// ▼ ▼
// ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐
// │ │ │ │ │ │ │ │ │ │ │ │
// │ Block │ │ Block │ │ Block │ │ Block │ │ Block │ │ Block │
// │ │ │ │ │ │ │ │ │ │ │ │
// └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘
//
//
//
//
//
//
//
//
//
//
//
//
// ┌────────────────────────────────────────────────────────────┐
// │ Hitting replay will cause the queue to be rerun in FIFO │
// └────────────────────────────────────────────────────────────┘
//
// ╳ │
// ╳ │
// ╳ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─replay ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─▶ │
// ╳ │
// ╳ │
// ╳ │
// ▼ ▼
// ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐
// │ │ │ │ │ │ │ │ │ │ │ │
// │ Block │ │ Block │ │ Block │ │ Block │ │ Block │ │ Block │
// │ │ │ │ │ │ │ │ │ │ │ │
// └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘
//
//
//
//
//
//
//
//
//
//
//
//
//
// ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────┐
// │ Should new blocks be added during replay mode they will simply be appended and run in FIFO order │
// └─────────────────────────────────────────────────────────────────────────────────────────────────────────┘
//
// ╳ │
// ╳ │
// ╳ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─▶ │
// ╳ replay │
// ╳ │
// ╳ │
// ▼ ▼
// ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐
// │ │ │ │ │ │ │ │ │ │ │ │ │ │
// │ Block │ │ Block │ │ Block │ │ Block │ │ Block │ │ Block │ │ Block │
// │ │ │ │ │ │ │ │ │ │ │ │ │ │
// └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘
Shaheen Ghiassy, [email protected]
thread-safe-block-queue is available under the MIT license. See the LICENSE file for more info.