beef331 / website

Code for the official Nim programming language website
https://nim-lang.org
18 stars 1 forks source link

Loony #31

Closed shayanhabibi closed 2 years ago

shayanhabibi commented 2 years ago

Name: Loony

Author: cabboose, disruptek

Posting:

Multi-threading can be a headache. Especially when it’s so difficult to organise and efficiently communicate between your worker threads. Most of the time you end up with a data-racey unsafe hot mess. Why does it have to be so difficult to safely pass reference objects between threads with some level of determinism? Locks are cumbersome; you have to consider all the possibilities of dead-locking and friends!

Do you wish there was some simple and flexible solution to your multi-threaded nightmare?

Say hello to this loo-natic: Loony. Loony is a lock-free (no dead locks!) multi-producer multi-consumer leak-free FIFO queue! Don’t be concerned by the details of having to distribute work between threads using multiple queues when you can use loony.

Loony can handle up to 32, 255 consumer and 64, 610 producer threads lock-free (default is 512/1025; see README for details).

Any ref object can be passed through loony. The API? Dead simple:

import loony

type
  YourRefObjectHere = ref object
var ruhroh = new YourRefObjectHere

var lqueue = newLoonyQueue[YourRefObjectHere]()
# Setting up your loony queue is as simple as this!

lqueue.push ruhroh
# Don’t be concerned with having to get Consumer/Producer permissions!
# Do what you were born to do and ~move it move it ~ move it move it

var scooby = lqueue.pop()

Are you worried about memory validity? Don’t be! If you’re not too savvy, the standard push and pop api use atomic thread fences to ensure your CPU caches are synchronised.

Does this have performance costs though? Sure does; however this will be removed with future iterations of the project in conjunction with development of CPS.

You can always use unsafePush and unsafePop if you can handle the memory synchronisation yourself! The only difference is that we don’t call atomic thread fences for you!