Comments (7)
- 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.
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.
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.
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.
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.
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.
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)
- Related stuff worth looking at before implementation :) HOT 1
- Fail to deallocate consumed nodes HOT 1
- Introduce balls
- [ORC] Crash when GC attempts to collect element HOT 4
- Debugging helpers
- thread sanitizer does not agree with loony HOT 5
- [CHORE] Refactor alias types
- Example code errors HOT 8
- [NIM] Thread completion triggers thread sanitizer warnings HOT 2
- [LOONY] Memory Leak 0.1.3
- introduce queue state? HOT 2
- [MacOS/Darwin] Emulating Futex behaviour HOT 1
- SIGSEGV on q.isEmpty HOT 1
- SIGSEGV: Illegal storage access. (Attempt to read from nil?) HOT 4
- Ward Segfaults HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from loony.