Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

This reduces the friction, but doesn't eliminate the needles back and forth. Breaking context locality is just bad

The threads example in another comment is way better



sort by: page size:

That's not too bad assuming that only that thread is affected.

I can't think of another way to do it that wouldn't effectively reduce to this anyway.


Don't think ThreadLocals help when you actually need to share state between threads.

I furrowed my brow at that, too, but then I realized what I think he meant. If you use multiple threads that touch the same data in memory, that will yield better locality than if you had to marshall data across the process boundary.

It sounds like this could be a behind-the-scenes feature to optimize ThreadLocal class. It doesn't seem like the type of thing you'd use directly.

Sure, just be sure to avoid threadlocals and pass them around explicitly!

This doesn't use threading, just a thread-local variable (i.e. a variable that points to different memory locations for each thread) to make the whole thing threadsafe.

You're absolutely right, I've corrected this. Thank you!

I had originally written about threading and locality but it made the post too long and complicated, so I cut it out for the final draft. You can see remnants of it if you check the HTML comments in the post :D


You could use thread_local variables these days for re-entrancy instead of the ctx->i idea.

That imposes an uncalled for runtime overhead. A better way would be to implement it using code-patching, by overwriting the instructions at specific pre-determined locations which will then cause the thread to halt when executed.

Not tearing, but it could reorder the assignment so that it happens someplace else than you think it will. For this kind of thread synchronization memory barriers should be used at least.

Couldn’t it work if each threads only touch thread-specific data structures?

Did you use WAL?

But in general, funelling writes into one thread is the way to get around it


You will have to tune your code to need as little shared state across threads as you can. It's not fun, but tuning code at this level rarely is.

Yes, exactly (and why I described it as limited).

The physics thread (with interleaved joystick read code on PC) did have to yield back to the main/graphics thread. That was no issue in our case; I've not thought deeply about it, but it seems like the method is likely extensible to do context switches among multiple [unaware/non-cooperating] threads.


I had no idea this feature existed! Does it behave usefully in a multithreaded context?

> In theory you could take thread priority into account by observing and resetting the dirty/accessed bits every context switch, but this would be unlikely to be worth it.

That would be most likely terrible (as you suggest). You might have better luck giving each thread a copy of the process page table, and keeping that in sync somehow (which also sounds terrible, but might be more workable)


You can get that effect by pinning processors to threads and you can do the sync yourself with a barrier (like Java CyclicBarrier, not a memory barrier.)

The threads could take a pointer/reference to the "stop variable" and use the reference instead of accessing the global variable directly. That allows you to mitigate many problems of global variables. Sorry, if you already do this.
next

Legal | privacy