Unintentional sharing?

2024-06-06 Thread Andy Valencia via Digitalmars-d-learn
I was using instance initialization which allocated a new object. My intention was this initialization would happen per-instance, but all instances appear to share the same sub-object? That is, f1.b and f2.b appear to point to a single object? Obviously I moved the new into the initializer

Re: How to pass in reference a fixed array in parameter

2024-06-04 Thread Andy Valencia via Digitalmars-d-learn
On Tuesday, 4 June 2024 at 12:22:23 UTC, Eric P626 wrote: I tried to find a solution on the internet, but could not find anything, I stumble a lot on threads about Go or Rust language even if I specify "d language" in my search. Aside from the excellent answer already present, I wanted to

Re: Socket and spawn()

2024-06-02 Thread Andy Valencia via Digitalmars-d-learn
On Sunday, 2 June 2024 at 17:46:09 UTC, bauss wrote: If anything you should use a thread pool that each handles a set of sockets, instead of each thread being a single socket. Yup, thread pool it is. I'm still fleshing out the data structure which manages the incoming work presented to the

Re: Socket and spawn()

2024-05-31 Thread Andy Valencia via Digitalmars-d-learn
On Friday, 31 May 2024 at 16:59:08 UTC, Jonathan M Davis wrote: Strictly speaking, unless you're dealing with a module or static-level variable, the object is not in TLS. It's treated as thread-local by the type system, and the type system will assume that no other thread has access to it, but

Re: Socket and spawn()

2024-05-31 Thread Andy Valencia via Digitalmars-d-learn
On Friday, 31 May 2024 at 19:48:37 UTC, kdevel wrote: Have you taken into consideration that each of the (pre-spawned) threads can call accept()? Your program may also accept in multiple processes on the same socket. [1] Yes, but I am planning on some global behavior--mostly concerning

Socket and spawn()

2024-05-31 Thread Andy Valencia via Digitalmars-d-learn
I'm coding a server which takes TCP connections. I end up in the main thread with .accept() which hands me a Socket. I'd like to hand this off to a spawn()'ed thread to do the actual work. Aliases to mutable thread-local data not allowed. Is there some standard way to get something

Re: Problem with clear on shared associative array?

2024-05-27 Thread Andy Valencia via Digitalmars-d-learn
On Monday, 27 May 2024 at 04:04:03 UTC, mw wrote: Pls NOTE: it is a `sharded` (meaning trunk-ed) NON-concurrent map, not `shared` concurrent map. Assuming I put it in shared memory, in what way is it not able to be used concurrently? It seems to have the needed lock operations? Thanks,

Re: Problem with clear on shared associative array?

2024-05-26 Thread Andy Valencia via Digitalmars-d-learn
On Sunday, 26 May 2024 at 20:00:50 UTC, Jonathan M Davis wrote: No operation on an associative array is thread-safe. As such, you should not be doing _any_ operation on a shared AA without first locking a mutex to protect it. Then you need to cast away shared to access or mutate it or do

Problem with clear on shared associative array?

2024-05-26 Thread Andy Valencia via Digitalmars-d-learn
The following code fails to compile; it appears from the error message that the library's clear() function is not ready to act on a shared AA? synchronized class F { private: string[int] mydict; public: void clear() { this.mydict.clear(); } } void main() { auto f =

Parallel safe associative array?

2024-05-24 Thread Andy Valencia via Digitalmars-d-learn
I was playing with parallel programming, and experienced "undefined behavior" when storing into an Associative Array in parallel. Guarding the assignments with a synchronized barrier fixed it, of course. And obviously loading down your raw AA with thread barriers would be foolish. But this

Re: FIFO

2024-05-13 Thread Andy Valencia via Digitalmars-d-learn
On Sunday, 12 May 2024 at 22:03:21 UTC, Ferhat Kurtulmuş wrote: https://dlang.org/phobos/std_container_slist.html This is a stack, isn't it? LIFO? Ahh yes. Then use dlist Thank you. I read its source, and was curious so I wrote a small performance measurement: put 10,000 things in a FIFO,

Re: FIFO

2024-05-12 Thread Andy Valencia via Digitalmars-d-learn
On Sunday, 12 May 2024 at 19:45:44 UTC, Ferhat Kurtulmuş wrote: On Saturday, 11 May 2024 at 23:44:28 UTC, Andy Valencia wrote: I need a FIFO for a work scheduler, and nothing suitable jumped out at me. ... https://dlang.org/phobos/std_container_slist.html This is a stack, isn't it? LIFO?

FIFO

2024-05-11 Thread Andy Valencia via Digitalmars-d-learn
I need a FIFO for a work scheduler, and nothing suitable jumped out at me. I wrote the following, but as a newbie, would be happy to receive any suggestions or observations. TIA! /* * fifo.d * FIFO data structure */ module tiny.fifo; import std.exception : enforce; const uint GROWBY

Re: "in" operator gives a pointer result from a test against an Associative Array?

2024-05-10 Thread Andy Valencia via Digitalmars-d-learn
On Friday, 10 May 2024 at 16:33:53 UTC, Nick Treleaven wrote: Arrays evaluate to true in boolean conditions if their `.ptr` field is non-null. This is bug-prone and I hope we can remove this in the next edition. ... A string literal's `.ptr` field is always non-null, because it is

Re: "in" operator gives a pointer result from a test against an Associative Array?

2024-05-10 Thread Andy Valencia via Digitalmars-d-learn
On Friday, 10 May 2024 at 03:07:43 UTC, Steven Schveighoffer wrote: Yes, we say that a type has "truthiness" if it can be used in a condition (`while`, `if`, `assert`, etc). So if I may ask for one more small clarification... WRT "truthiness", I've observed that empty arrays are treated as

Re: "in" operator gives a pointer result from a test against an Associative Array?

2024-05-09 Thread Andy Valencia via Digitalmars-d-learn
On Friday, 10 May 2024 at 00:40:01 UTC, Meta wrote: Yes. The reason for this is that it avoids having to essentially do the same check twice. If `in` returned a bool instead of a pointer, after checking for whether the element exists (which requires searching for the element in the

"in" operator gives a pointer result from a test against an Associative Array?

2024-05-09 Thread Andy Valencia via Digitalmars-d-learn
tst7.d(6): Error: cannot implicitly convert expression `e in this.members` of type `bool*` to `bool` tst7.d(15): Error: template instance `tst7.Foo!uint` error instantiating I'm getting this for this bit of source (trimmed from the bigger code). I switched to this.members.get(e, false) and

Re: TIL: statically initializing an Associative Array

2024-05-06 Thread Andy Valencia via Digitalmars-d-learn
On Tuesday, 7 May 2024 at 01:14:24 UTC, Steven Schveighoffer wrote: On Tuesday, 7 May 2024 at 00:10:27 UTC, Andy Valencia wrote: I had a set of default error messages to go with error code numbers, and did something along the lines of: string[uint] error_text = [ 400: "A message",

TIL: statically initializing an Associative Array

2024-05-06 Thread Andy Valencia via Digitalmars-d-learn
I had a set of default error messages to go with error code numbers, and did something along the lines of: string[uint] error_text = [ 400: "A message", 401: "A different message" ]; and got "expression is not a constant" I eventually found this discussion:

Re: Challenge Tuples

2024-04-26 Thread Andy Valencia via Digitalmars-d-learn
On Friday, 26 April 2024 at 13:25:34 UTC, Salih Dincer wrote: You have a 5-item data tuples as Tuple(1, 2, 3, [1, 3], 5) and implement the sum (total = 15) with the least codes using the sum() function of the language you are coding... My Python solution (function named dosum to avoid

Re: Making one struct work in place of another for function calls.

2024-04-16 Thread Andy Valencia via Digitalmars-d-learn
On Wednesday, 17 April 2024 at 03:13:46 UTC, Liam McGillivray wrote: Is there a way I can replace "`TypeB`" in the function parameters with another symbol, and then define that symbol to accept `TypeB` as an argument, but also accept `TypeA` which would get converted to `TypeB` using a

Re: mmap file performance

2024-04-15 Thread Andy Valencia via Digitalmars-d-learn
On Monday, 15 April 2024 at 08:05:25 UTC, Patrick Schluter wrote: The setup of a memory mapped file is relatively costly. For smaller files it is a net loss and read/write beats it hands down. Interestingly, this performance deficit is present even when run against the largest conveniently

Re: mmap file performance

2024-04-11 Thread Andy Valencia via Digitalmars-d-learn
On Thursday, 11 April 2024 at 14:54:36 UTC, Steven Schveighoffer wrote: For a repeatable comparison, you should provide the code which does 1MB reads. With pleasure: import std.stdio : writeln, File, stderr; const uint BUFSIZE = 1024*1024; private uint countnl(File f) { uint res = 0;

mmap file performance

2024-04-10 Thread Andy Valencia via Digitalmars-d-learn
I wrote a "count newlines" based on mapped files. It used about twice the CPU of the version which just read 1 meg at a time. I thought something was amiss (needless slice indirection or something), so I wrote the code in C. It had the same CPU usage as the D version. So...mapped files,

Re: Why does Nullable implicitly casts when assigning a variable but not when returning from a function?

2024-04-10 Thread Andy Valencia via Digitalmars-d-learn
On Wednesday, 10 April 2024 at 20:41:56 UTC, Lettever wrote: ``` import std; Nullable!int func() => 3; void main() { Nullable!int a = 3; //works fine Nullable!int b = func(); //does not compile } Why make func() Nullable? It just wants to give you an int, right? Making it a

Opinions on iterating a struct to absorb the decoding of a CSV?

2024-03-28 Thread Andy Valencia via Digitalmars-d-learn
I wanted a lightweight and simpler CSV decoder. I won't post the whole thing, but basically you instantiate one as: struct Whatever { ... } ... f = File("path.csv", "r"); auto c = CSVreader!Whatever(f); foreach (rec; c) { ... CSVreader is, of course, templated: struct

Re: varargs when they're not all the same type?

2024-03-16 Thread Andy Valencia via Digitalmars-d-learn
On Friday, 15 March 2024 at 00:11:11 UTC, Andy Valencia wrote: (varargs & friends) Which statement leads me to section 77.2 of "Programming in D", and now I am deep into the mechanisms behind what you have very kindly shared. Thank you once more. As some fruits of my labors here, below is

Re: varargs when they're not all the same type?

2024-03-14 Thread Andy Valencia via Digitalmars-d-learn
On Thursday, 14 March 2024 at 23:13:51 UTC, Basile B. wrote: ... However explicit instantiation can take whatever is known at compile time, such as constant expressions or even certain static variables. So that is rather called an `alias sequence` in D. Which statement leads me to section

Re: varargs when they're not all the same type?

2024-03-14 Thread Andy Valencia via Digitalmars-d-learn
On Thursday, 14 March 2024 at 18:05:59 UTC, H. S. Teoh wrote: ... The best way to do multi-type varags in D is to use templates: import std; void myFunc(Args...)(Args args) { Thank you. The first parenthetical list is of types, is it not? I can't find anywhere which says

varargs when they're not all the same type?

2024-03-14 Thread Andy Valencia via Digitalmars-d-learn
Can somebody give me a starting point for understanding varadic functions? I know that we can declare them int[] args... and pick through whatever the caller provided. But if the caller wants to pass two int's and a _string_? That declaration won't permit it. I've looked into the

Re: static functions?

2024-03-11 Thread Andy Valencia via Digitalmars-d-learn
On Monday, 11 March 2024 at 16:25:13 UTC, Jonathan M Davis wrote: ... But what exactly static means varies based on the context. Thank you for the list! But none of those appear to apply to a function defined in the outermost scope of the module. Is static accepted here--but has no actual

static functions?

2024-03-11 Thread Andy Valencia via Digitalmars-d-learn
Leveraging my knowledge of C, I assumed a "static" function would be hidden outside of its own source file. I can't find any statement about the semantics of a static function in the documentation, and in practice (ldc2 on Linux) it doesn't hide the function? file tst.d: import std.stdio

Re: Question on shared memory concurrency

2024-03-05 Thread Andy Valencia via Digitalmars-d-learn
On Monday, 4 March 2024 at 18:08:52 UTC, Andy Valencia wrote: For any other newbie dlang voyagers, here's a version which works as expected using the system memory allocator. On my little i7 I get 1.48 secs wallclock with 5.26 CPU seconds. ... Using a technique I found in a unit test in

Re: Question on shared memory concurrency

2024-03-04 Thread Andy Valencia via Digitalmars-d-learn
On Monday, 4 March 2024 at 16:02:50 UTC, Andy Valencia wrote: On Monday, 4 March 2024 at 03:42:48 UTC, Richard (Rikki) Andrew Cattermole wrote: ... I still hope to be able to share memory between spawned threads, and if it isn't a shared ref of a shared variable, then what would it be? Do I

Re: Question on shared memory concurrency

2024-03-04 Thread Andy Valencia via Digitalmars-d-learn
On Monday, 4 March 2024 at 03:42:48 UTC, Richard (Rikki) Andrew Cattermole wrote: A way to do this without spawning threads manually: ... Thank you! Of course, a thread dispatch per atomic increment is going to be s.l.o.w., so not surprising you had to trim the iterations. Bug I still

Question on shared memory concurrency

2024-03-03 Thread Andy Valencia via Digitalmars-d-learn
I tried a shared memory parallel increment. Yes, it's basically a cache line thrasher, but I wanted to see what's involved in shared memory programming. Even though I tried to follow all the rules to make true shared memory (not thread local) it appears I failed, as the wait loop at the end