<xyhuvud>
Deques are not thread safe though. I'd look for ways to restructure so that channels, or channels with a capacity, is possible.
<FromGitter>
<moe:busyloop.net> oh, somehow i had assumed they are
<FromGitter>
<moe:busyloop.net> should def put some warnings about what is and isn't threadsafe in the docs before MT lands 😅
<FromGitter>
<moe:busyloop.net> as long as MT is off, Deque across fibers should work tho, right? (i think i've relied on that somewhere in the past 🤔)
<FromGitter>
<jrei:matrix.org> Lots of data structures aren't thread safe in Crystal
<FromGitter>
<jrei:matrix.org> The safe way is to use channels
<FromGitter>
<moe:busyloop.net> hm yea, looks like Deque isn't fiber-safe even in the little test i just made 😞
<FromGitter>
<moe:busyloop.net> err disregard my last msg. Deque looks fibersafe (at least in the little test i just made)
<FromGitter>
<azurelmao> > as long as MT is off, Deque across fibers should work tho, right? (i think i've relied on that somewhere in the past 🤔) ⏎ ⏎ what if MT is on
<FromGitter>
<azurelmao> which it is
<FromGitter>
<moe:busyloop.net> for Deque you can try it out with this snippet: https://carc.in/#/r/ejv9
<FromGitter>
<moe:busyloop.net> (likely wanna use a larger value that `10.times` - just had to made that small cause carc.in runs on raspberry)
<FromGitter>
<moe:busyloop.net> my guess is that on MT it will crash & burn unless you uncomment the mutex
<FromGitter>
<moe:busyloop.net> but don't have a MT binary to test myself
<FromGitter>
<azurelmao> > for Deque you can try it out with this snippet: https://carc.in/#/r/ejv9 ⏎ ⏎ which parts of the code can I remove? I don't quite understand what's going on there
<FromGitter>
<moe:busyloop.net> nothing, just run `crystal snippet.cr` with or without MT. *with* MT it'll likely throw some error, *without* MT it works.
<FromGitter>
<azurelmao> oh, I thought you gave me some example code to use
<FromGitter>
<moe:busyloop.net> to make it also work on MT, you can uncomment the lines that are currently commented out (those wrap the deque access in a lock)
<FromGitter>
<azurelmao> seems to work even without the mtx
<FromGitter>
<moe:busyloop.net> no, sorry, i was just testing my assumption of Deque being fibersafe without using locks (it is). ⏎ if you want to use it with MT enabled then you need to wrap it in locks (the part that is commented out in my snippet).
<FromGitter>
<moe:busyloop.net> even when you increase `10.times` -> `12456.times`? 🤔
<FromGitter>
<azurelmao> oh, nvm I used "Run current file" to run it which is without mt enabled
<FromGitter>
<moe:busyloop.net> right. even if it happens to work in MT, i wouldn't rely on it. (if you want to use Deque in MT then use locks)
<FromGitter>
<azurelmao> is there an easier way if I just want to transmit Bool?
<FromGitter>
<azurelmao> like i wont be putting any data in the deques
<FromGitter>
<moe:busyloop.net> i'm not familiar enough with MT to say for sure. ⏎ maybe you can just set `@foo = true` from within the thread (i just don't know if it blows up when multiple threads do that simultaneously)
<FromGitter>
<moe:busyloop.net> fwiw, MT is still marked as "preview" for a reason. if you wanna use it then i'd rather play it safe and either use `Channel` or liberally wrap things in mutexes.
brw has quit [Quit: Ping timeout (120 seconds)]
brw has joined #crystal-lang
<FromGitter>
<jrei:matrix.org> To be sure, you use `-preview_mt`?
<FromGitter>
<azurelmao> -Dpreview_mt
<FromGitter>
<azurelmao> is it different?
<FromGitter>
<azurelmao> @moe:busyloop.net btw I made a mutex and put .synchronize in the places where i assign and access it
<FromGitter>
<azurelmao> and now it works correctly
<FromGitter>
<azurelmao> oh I just realized i didnt tell you what I needed it for