3 ms·
It's clear that you know a lot about this stuff, but I think maybe not quite enough. The Linux kernel retains the term "task" for "execution context", and the
by PaulDavisThe1st 5y ago
It's clear that you know a lot about this stuff, but I think maybe not quite enough.
The Linux kernel retains the term "task" for "execution context", and the term "thread" is used exclusively in user space purely because of programmer conventions (pthreads, specifically). This makes a limited amount of of sense because "thread" has also come to mean things that are very definitely not full execution tasks (user-space and/or so-called "green" threads). This overloading of the terminology in user-space is not reflected in kernels, but allows for developers to play with alternate implementations without changing their fundamental terminology. After all, there some things where user-space threads are precisely what you want (and these absolutely do not correspond to kernel tasks).
> This is almost never what you want.
I would beg to differ. I've been writing multithreaded code in C++ for nearly 30 years, and I don't remember any time that I wanted to join on a group of threads. My current project (20 years old) is heavily multithreaded (typically 20-60 threads) and there is nowhere that a thread primitive to join on a group of threads would make our lives easier. The only context where I could even think of using it might be to wait on a thread pool, but in general this tends to be unnecessary and if it is, can be trivially handled without a multi-join.
> Already in 1965, PL/I had a wait that could do anything that can be done with WaitForMultipleEvents/WaitForSingleEvent,
WaitForMultipleEvents/WaitForSingleEvent are not part of the C or C++ language, but Windows system calls. While their semantics are very powerful and useful (which obviously would thus apply to PL/1's wait), their absence cannot be laid at the feet of C or C++: no Unix-like OS has had this operation, ever (it is almost possible with contemporary Linux).
You could make the argument that it's not part of pthreads because this operation was never a part of posix, and so pthreads could not build on it (you can use those calls on Windows even in a single-threaded task, and that would be true in a POSIX system if the call existed). So it really has almost nothing to do with pthreads at all, other than that one could imagine a pthread wanting to block on "wait-for-something-anything".
- monocasa 5y agoLinux exports the concept of threads in it's user facing ABI. You can see this in "Thread Group ID" and "Thread ID" values.
- adrian_b 5y ago> The Linux kernel retains the term "task" for "execution context" This has historical roots from the first UNIX versions written for DEC computers. Even if the UNIX authors usually preferred the term "process", in the DEC documentation for their computers and operating systems the term "task" was always used for "process". So the term was also used in UNIX in various places. > I don't remember any time that I wanted to join on a group of threads This is not surprising, because there are many kinds of multi-threaded applications and many styles of programming them. I do not doubt that what you say is correct for you applications, but my experience happened to be opposite. I have never encountered a case when I wanted to join a single thread, but I have encountered a lot of cases when I wanted to join any of a group of threads (e.g. for keeping a number of active threads matching the number of available cores) and also a less number of cases when I wanted to join all of a group of threads, typically at the end of an operation. The latter case is less important, because it can be done by repeating a join with a single thread, even if that is much less efficient than a wait that waits for all. > are not part of the C or C++ language, but Windows system calls This is precisely what I have already said in my first post, i.e. that standardized languages like C/C++ are forced to specify only the minimal features that are available on all operating systems, so they cannot include WaitForMultipleEvents, while PL/I was free of such portability concerns, so it could specify more powerful features.
- PaulDavisThe1st 5y ago> This is precisely what I have already said in my first post, i.e. that standardized languages like C/C++ are forced to specify only the minimal features that are available on all operating systems, so they cannot include WaitForMultipleEvents, while PL/I was free of such portability concerns, I think you missed my point. WaitForMultipleEvents is not part of a thread API on any platform. It's a part of the platform API, and is used by single-threaded and multi-threaded code. There's no reason for pthreads (or any other thread API) to represent this system call, because the system call either exists, and can be used directly, or does not exist, and cannot be used. In essence, you're really just noting that POSIX (not pthreads) never had a wait-for-just-about-anything API. That's a legitimate complaint, just not very relevant for multithreaded programming. > This is not surprising Well, given that you said "This is almost never what you want.", I'd count it as least a little surprising. My point was that multi-join is not "almost never what you want", but has always been "useful in certain contexts". I have never come across a multi-join API that blocks until all threads have completed (they typically return when any of the specified threads completes), and so the difference in efficiency for this version of multi-join is essentially identical to a loop+single-join. >This has historical roots from the first UNIX versions written for DEC computers. I don't see much evidence for this claim. task_t exists in early versions of AIX and Mach, and the terminology was already common in Multics (as you know). I don't think that Linux' use of task_t has any relationship to the Ultrix use, but maybe you have some specific insight here?
- otabdeveloper4 5y ago> is heavily multithreaded (typically 20-60 threads) Hm. For me "heavily multithreaded" is 1000 threads and more. That said, I just detach them, there's no real reason to wait for a thread.
- PaulDavisThe1st 5y agounless you've got 1000 cores OR are severely i/o bound, 1000 threads seems mostly useless. OTOH, "severely i/o bound" tends to describe client/server computing quite well, so maybe that's just what you need. my stuff isn't client/server computing, my threads are generally compute bound, and having (lots) more than there are cores would be counter-productive.
- otabdeveloper4 5y ago1000 threads blocking on I/O operations is the same as an epoll() over 1000 file descriptors under the hood, except you also get pre-emption guarantees from the kernel. Yeah, so basically this. Except that "I/O bound" usually implies critical moments of computation too, and pre-emption is really nice here.