D atomics
How does one really use atomics in D? shared string[int] AA; AA[s[2]].atomicStore(s[1]); causes a hangup. If I just assign it "works": AA[s[2]] = s[1]; I've had similar issues where: atomicOp!"+="(X[Y],1); fails and I have to use X[Y].atomicStore(1); or atomicOp!"+="(count,1); // fails (does not increment count) so I have to create a new variable int rcount = atomicFetchAdd(count,1); and this will increment count. In fact, I don't know what works or what doesn't except by trial and error. I don't even know how to go about thinking about these issues. In my mind, when using multiple threads and shared variables I try to use atomics to avoid any race conditions. Usually this involves incrementing or storing values. I try to use atomics that replace the single threaded concept but they do not always seem to work. Are there any known issues or is it just me? Should it just work? is there any good reference that gets ones mind right for multithreading? I have some idea but I rarely do multithreading until lately(I guess because I now have the HP to care). I think the main issue I tend to have is trying to convert single threaded code in to multiple threads and missing certain key issues. E.g., I was doing a parallel for and it wasn't really working(it seems the task pool stalls the entire pool until all tasks are complete): for(s; parallel(arr)) { // do stuff with s. } So I converted to using threads: for(s; arr) { new Thread({ // do stuff with s. }).start(); } // slightly more complex as I limited the total number of threads But as I spun up threads I would get different threads working on the same s. I imagined that this likely had something to do with the variable being on the stack being reused. so I wrapped the thread in a function: for(ss; arr) { void foo(typeof(ss) s) { new Thread({ // do stuff with s. }).start(); } foo(ss); } and this seemed to solve the problem. I imagine this is because ss is duped by the function call essentially detaching it from the loop code so it is unique across the thread. But I guess I need to learn exactly how to deal with all these problems rather than just hacking away.
Re: Operator "+=" overloading for class?
On Sunday, 17 December 2023 at 04:13:20 UTC, Ki Rill wrote: I am trying to overload `opOpAssign` for my class. The code compiles, but it does not seem to be working. ```d // binary operations have already been implemented for Value // i need +=, -=, *=, /= auto opOpAssign(string op)(Value rhs) { mixin("return this" ~ op ~ "rhs;"); } auto opOpAssign(string op)(in ElementType rhs) { mixin("return this" ~ op ~ "rhs;"); } ``` What am I missing here? Full project code can be found [here](https://github.com/rillki/tiny-grad). Perhaps: ```d auto opOpAssign(string op)(Value other) { mixin("this.v " ~ op ~ "= other.v;"); return this; } ```
Re: D is nice whats really wrong with gc??
On Mon, Dec 18, 2023 at 04:44:11PM +, Bkoie via Digitalmars-d-learn wrote: [...] > but what is with these ppl and the gc? [...] It's called GC phobia, a knee-jerk reaction malady common among C/C++ programmers (I'm one of them, though I got cured of GC phobia thanks to D :-P). 95% of the time the GC helps far more than it hurts. And the 5% of the time when it hurts, there are plenty of options for avoiding it in D. It's not shoved down your throat like in Java, there's no need to get all worked up about it. T -- Computerese Irregular Verb Conjugation: I have preferences. You have biases. He/She has prejudices. -- Gene Wirchenko
D is nice whats really wrong with gc??
just look at this i know this is overdesign im just trying to get a visual on how a api can be design im still new though but the fact you can build an api like this and it not break it is amazing. but what is with these ppl and the gc? just dont allocate new memory or invoke, you can use scopes to temporry do stuff on immutable slices that will auto clean up the list goes on and you dont need to use pointers at all...!! i honesty see nothing wrong with gc, ofc d has some downsides, docs not very good compare to some other lang. ide support not great but it works sometimes i use helix and lapce and maybe sometimes intellj it works better in helix though. and of d is missing some minor libraries ``` import std.stdio: writeln, readln; auto struct Game { string title; private Board _board; private const(Player)[] _players; final auto load(T)(T any) { static if (is(T == Player)) { _pushPlayer(any); } return this; }; final auto play() {assert(_isPlayersFull, "require players is 2 consider removing"); "playing the game".writeln;}; final auto _end() {}; auto _currentPlayers() const {return _players.length;} enum _playerLimit = 2; auto _isPlayersFull() const {return _currentPlayers == _playerLimit;} import std.format: format; auto _pushPlayer(T: Player)(T any) { if (_isPlayersFull) assert(false, "require %s players".format(_playerLimit)); _players.reserve(_playerLimit); _players ~= any; } } private struct Board {} enum symbol {none, x, o} private struct Player {const(string) _name; symbol _hand; @disable this(); public this(in string n) {_name = n;}} alias game = Game; alias player = Player; alias board = Board; auto main() { import std.string: strip; game() .load(player(readln().strip)) // .matchmake .load(player(readln().strip)) .play; } ```
Re: Operator "+=" overloading for class?
On Monday, 18 December 2023 at 04:49:53 UTC, novice2 wrote: On Monday, 18 December 2023 at 03:39:16 UTC, Ki Rill wrote: On Sunday, 17 December 2023 at 07:05:12 UTC, Adam D. Ruppe wrote: On Sunday, 17 December 2023 at 04:13:20 UTC, Ki Rill wrote: [...] check what `op` is. pretty sure it is "+" not "+=" so your element isnt' saved anywhere. also a bit iffy there isn't a member here to work on Yes, op is '+'. What do you mean by it isn't saved anywhere? your code just return result value, but it should not return but save result to "this" see example at https://dlang.org/spec/operatoroverloading.html#index_op_assignment I get an error, but don't understand why. ```d auto opOpAssign(string op)(Value rhs) { this = this + rhs; return this; } // Error: `this` is not an lvalue and cannot be modified ```