# Threads for calculations

**URL:** <https://ziggit.dev/t/threads-for-calculations/7068>\
**Category:** Brainstorming\
**Created:** [November 29, 2024, 1:18pm UTC](https://ziggit.dev/t/threads-for-calculations/7068 "2024-11-29T13:18:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [November 29, 2024, 1:18pm UTC](https://ziggit.dev/t/threads-for-calculations/7068/1 "2024-11-29T13:18:13Z")

</div>

I have a scrabble algorithm which looks basically like this.  
These 4 routines must be sequentially executed.

```zig
for (Board.ALL_SQUARES) |square| self.gen(board, square, NORMAL, .Horizontal);
for (Board.ALL_SQUARES) |square| self.gen(board, square, NORMAL, .Vertical);
for (Board.ALL_SQUARES) |square| self.gen(board, square, CROSSWORDS, .Horizontal);
for (Board.ALL_SQUARES) |square| self.gen(board, square, CROSSWORDS, .Vertical);

```

Inside the generator are some square-info write things going on during `gen`.  
The board is not touched and only read from.  
And then I have the “global” movelist which is written to with each move found.

I never seriously worked with parallel threads but I would like to give it a try.

I was wondering: when processing horizontal or vertical the 15 rows the results are _completely_ independent of eachother. Would that be a good candidate idea for using threads per row / column? Would it actually speed up things?

If so, are there some basic thread tips people have here?  
The only thing I really know is that threads must not write in parallel to the same memory area.

---

<div class="post-metadata">

**Author:** ![daredemo](https://ziggit.dev/user_avatar/ziggit.dev/daredemo/32/3595_2.png) [@daredemo](https://ziggit.dev/u/daredemo)\
**Post date:** [November 29, 2024, 1:32pm UTC](https://ziggit.dev/t/threads-for-calculations/7068/2 "2024-11-29T13:32:37Z")

</div>

Don’t forget that multi-threading adds an overhead. If you are doing very small tasks, you might actually get worse performance than just executing all in a single thread.

---

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [November 29, 2024, 2:55pm UTC](https://ziggit.dev/t/threads-for-calculations/7068/3 "2024-11-29T14:55:37Z")

</div>

true… I suspect the overhead is worse than the tasks.  
There I sit, with my 20 cores doing nothing 🙂

---

<div class="post-metadata">

**Author:** ![pachde](https://ziggit.dev/user_avatar/ziggit.dev/pachde/32/2485_2.png) [@pachde](https://ziggit.dev/u/pachde)\
**Post date:** [November 29, 2024, 8:44pm UTC](https://ziggit.dev/t/threads-for-calculations/7068/4 "2024-11-29T20:44:38Z")

</div>

Do you actually have a performance problem?

---

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [November 29, 2024, 10:19pm UTC](https://ziggit.dev/t/threads-for-calculations/7068/5 "2024-11-29T22:19:19Z")

</div>

no, i’m a performance freak and like experiments

---

<div class="post-metadata">

**Author:** ![daredemo](https://ziggit.dev/user_avatar/ziggit.dev/daredemo/32/3595_2.png) [@daredemo](https://ziggit.dev/u/daredemo)\
**Post date:** [November 30, 2024, 2:16am UTC](https://ziggit.dev/t/threads-for-calculations/7068/6 "2024-11-30T02:16:47Z")

</div>

then go on, experiment 😃

post your finding after you’ve got conclusive results 😛

---

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [November 30, 2024, 10:37am UTC](https://ziggit.dev/t/threads-for-calculations/7068/7 "2024-11-30T10:37:21Z")

</div>

> [@ericlang](#):
>
> If so, are there some basic thread tips people have…

I can experiment. I just asked the question to prevent me from taking 5 wrong experiment paths.

---

<div class="post-metadata">

**Author:** ![daredemo](https://ziggit.dev/user_avatar/ziggit.dev/daredemo/32/3595_2.png) [@daredemo](https://ziggit.dev/u/daredemo)\
**Post date:** [November 30, 2024, 10:58am UTC](https://ziggit.dev/t/threads-for-calculations/7068/8 "2024-11-30T10:58:43Z")

</div>

BTW, it’s not entirely true that you cannot write in parallel-ish. You can use mutex etc to make sure that each write happens when other writes do not happen.
