GithubHelp home page GithubHelp logo

Comments (7)

disruptek avatar disruptek commented on May 21, 2024 1
  • use refs
  • don't use locks
  • use atomics versus globals
  • don't access messages after they are enqueued
import std/[locks, os, atomics]
import loony

type
  Message = ref object
    value: string

let fifo = newLoonyQueue[Message]()
var terminate: Atomic[bool]


proc producer() {.thread.} =
  for i in 1..10:
    let msg = Message(value: "Message " & $i)
    echo "Producing ", repr(msg)
    fifo.push msg
    sleep(100)

proc consumer() {.thread.} =
  while not terminate.load:
    let item = fifo.pop
    if not item.isNil:
      echo "Consumed: ", repr(item)
    sleep(10)

# Create worker threads
var producerThread, consumerThread: Thread[void]

# Start worker threads
createThread(producerThread, producer)
createThread(consumerThread, consumer)

joinThread(producerThread)
terminate.store(true)
joinThread(consumerThread)

from loony.

shayanhabibi avatar shayanhabibi commented on May 21, 2024

I'm away until tomorrow and have not touched programming with a 5-foot pole in almost two years. @disruptek hopefully isn't as grouchy as I remember and can help.

Otherwise I think the principle error is that your Message is a Object and not a ref object. Therefore, the message it contains is instantaneously destroyed when you put it into the loony. Loony has nothing to do with memory allocation/deallocation outside of its own function. HOWEVER, we will do the appropriate memory protective functions for a native 'ref' object (within the means provided by std lib which is not atomic back when we made this).

Also no locks. Why are you using locks for a lockfree algorithm? You're essentially getting an F1 race car and then towing it with a truck to work and wondering why it takes you the same amount of time to get to work.

Please try compile with Message as a ref object.

type
  Message = ref object
    value: string

note: this assumes you are using ARC or ORC memory management paradigms.

Let me know how you go.

from loony.

shayanhabibi avatar shayanhabibi commented on May 21, 2024

I just realised you are initialising your message objects in a profoundly error-prone and obscure way.

Rather than this:
let Message = Message()

Do this:
let message = Message()

from loony.

shayanhabibi avatar shayanhabibi commented on May 21, 2024

I just realised there are fundamentally a few things wrong with your example code that I cannot provide a satisfactory correction for on my iPad. I'll provide you a corrected sample of code tomorrow (although my memory is notoriously bad and I might forget - don't hesitate to remind me).

from loony.

SirStone avatar SirStone commented on May 21, 2024

Thanks for the fast reply, I will learn more from this code of your than hours spent figuring out myself.

ok let me say that the let Message = Message() was a mistake cause by Copilot that I missed cause I was coding under the sun, visibility 20% my bad.
The rest of the suggestions...it's gold for me, I'm not aware of what Atomic is and I was not aware for the differences between = object and = ref object. Still my bad and ignorance is not an exscuse.
The locks, I've added those out of madness, let's just pretend it didn't happen >_>

I've already compiled and run your code, now I'm going to study it.

I want ask another basic question, what is the best method to check that the queue is not empty? I see that there's the isEmpty call but the documentations recites This operation should only be used by internal code. The response for this operation is not precise. suggesting to not use it.

I'm going out of topic, this issue can be closed

from loony.

shayanhabibi avatar shayanhabibi commented on May 21, 2024

Thanks @disruptek, much appreciated for that. @SirStone you have no reason to apologise. Everyone starts from somewhere and the nuances of nim can only be found through trial and error or by looking under the hood. I'll be happy to provide answers for all of the above in a discussion or some other format.

from loony.

shayanhabibi avatar shayanhabibi commented on May 21, 2024

I still firmly believe the following resource is invaluable for any acolyte nimmers: https://github.com/StefanSalewski/NimProgrammingBook

Dr Stefan Salewski has created a fantastic resource which covers all the nuances of nim fantastically.

from loony.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.