The following program simulates constant locking und unlocking of one
to jthread::hardware_concurrency() threadas with a std::mutex and a
futex. On my 16 core / 32 thread Zen4 system a futex is faster up to
5 threads constantly contending, but beyond the CPU time of the futex explodes and the conventional mutex is faster with Windows as and with
Linux.
Am 16.04.2024 um 23:14 schrieb Chris M. Thomasson:
Well, futex notify might have fast paths in and of itself. To be
prudent I would need to see how they implement it to allow a futex
notify by, every time. Fair enough?
I'm asking myself if it would be possible to have context-switching as
most as possible in userspace. If there would be a context-switch from
one thread of a process to another thread because a timeslice expired
the kernel should send a signal to the thread and the thread does the userspace context-switch by itself. Only if there's a context switch
to another process' thread or in kernel mode the kernel's scheduler
acts itself.
This would give the opportunity to have voluntary context switches
when doing locking much faster than trough the kernel, and voluntary
context switches usually happen with a much higher frequency that
there would be a real gain.
With Linux this would be possible trough signals and on Windows the
kernel could induce SEH-exceptions for a thread-switch.
Am 19.04.2024 um 04:03 schrieb Richard Damon:
Admittedly, if the kernel knows it is switching from one thread
to another in the same process it can do a lighter weight sort of
context-switch, but it still needs to deal with kernel space operations.
A context switch through the kernel is always expensive. A user
-level thread switch when blocking for a lock would be much faster.
Am 19.04.2024 um 15:41 schrieb Scott Lurndal:
Bonita Montero <Bonita.Montero@gmail.com> writes:
Am 19.04.2024 um 04:03 schrieb Richard Damon:
Admittedly, if the kernel knows it is switching from one threadA context switch through the kernel is always expensive. A user
to another in the same process it can do a lighter weight sort of
context-switch, but it still needs to deal with kernel space operations. >>>
-level thread switch when blocking for a lock would be much faster.
SVR4.2MP implemented a M-N thread model (M user threads mapped to
N kernel threads). Turned out not to work well.
The thing that I'm imaging is still 1:1 but if threads are in userspace >thread-switching would be done by the userspace.
Am 19.04.2024 um 16:48 schrieb Scott Lurndal:
Feel free to prototype it using setcontext(2), getcontext(2) and
makecontext(2).
I'd need the support of the kernel which should not make context
switches to another thread inside the same process if the thread
is within userspace. And the kernel should have to periodically
inject signals from the timer interrupt to userspace to make it
possible that the userspace-code does the involuntary context
-switch on its own. And I'd need synchronization-primitives like
mutexes and semaphores that would do the otherwise costly context
-switch in userspace; but that's rather easy compared to the kernel
support.
Am 19.04.2024 um 04:03 schrieb Richard Damon:
How do you "signal" a user-thread without doing a kernel operation
and a thread switch?
The signals for the involuntary userspace thread-switch would be sent
by a dedicated kernel-thread or by the timer interrupt. This would be
more expensive than a thread-switch through the timer interrupt but
as voluntary thread-switches have a much higher frequency this would
be outweighed.
Admittedly, if the kernel knows it is switching from one thread
to another in the same process it can do a lighter weight sort of
context-switch, but it still needs to deal with kernel space operations.
A context switch through the kernel is always expensive. A user
-level thread switch when blocking for a lock would be much faster.
Am 19.04.2024 um 20:27 schrieb Scott Lurndal:
Bonita Montero <Bonita.Montero@gmail.com> writes:
Am 19.04.2024 um 16:48 schrieb Scott Lurndal:
Feel free to prototype it using setcontext(2), getcontext(2) and
makecontext(2).
I'd need the support of the kernel which should not make context
switches to another thread inside the same process if the thread
is within userspace. And the kernel should have to periodically
inject signals from the timer interrupt to userspace to make it
possible that the userspace-code does the involuntary context
-switch on its own. And I'd need synchronization-primitives like
mutexes and semaphores that would do the otherwise costly context
-switch in userspace; but that's rather easy compared to the kernel
support.
https://www.kernel.org/
Feel free to modify the kernel to your heart's content.
Seems you don't understand the idead and you think this isn't
possible.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 57:10:02 |
Calls: | 10,397 |
Calls today: | 5 |
Files: | 14,067 |
Messages: | 6,417,448 |
Posted today: | 1 |