|
That isn't the usual way of doing things; the common approach is:
1. Create pool of threads at startup (3, 5, whatever).
2. Create two queues at startup (work-queue and results-queue, using a LL or DLL queue, not an array).
3. Each thread takes the next work item from work-queue, working to completion, and posting the results to results-queue.
4. The main thread simply enqueues work items to the work-queue, and waits on the results-queue (use a semaphore here) for results.
There's no blocking involved other than the wait-on-semaphore (and wait-on-mutex for queue modification).
|
|
|
|
|
That's actually what I ended up doing. Greg Utas made a similar comment as you and I had an aha moment. Before that, I had engineering tunnel vision wherein I was stuck on doing something like I had imagined but there was an easier way. Still, the code, while much simpler is still ridiculous. God bless the Task framework - of course using it this time would have defeated the purpose of what i was doing
Real programmers use butterflies
|
|
|
|
|
Yeah, multi-threading is a pain in the rear. Concurrent processing and parallel processing in general is a pain in the rear, so it's usually best to go with some common idiom.
Every single time I invented my own method of managing threads/tasks/processes I've regretted it.
Shared-memory concurrency is painful; my best experiences with parallel computations were on Erlang. Using those idioms in my projects in other languages made my life a great deal easier, and now I no longer worry about parallel computations because I skip the shared-memory approach altogether.
|
|
|
|
|
Is very important like multithread is used: language, instructions and function. For example, with C++ into a Win32(IDE Visual Studio), with omp instruction and loop function, concurrency take a single thread for settings, like number of cores available and number of thread that need. Into this loop, a "do", this threads not take consequential way(and number into "do", the same) but seem random, also not really random, because depend like thread work
|
|
|
|
|
Greg Utas wrote: Well, I'm a longstanding member of the choir that you're preaching to.
I agree. that's why if I had to do anything that really did heavy multi-threading (as a service or back-end type thing -- not just in a WinForm app) then I would use the new thing: Akka (which implements the Actor Model[^].
I've actually used it one time and it is quite amazing once you get past the learning curve.
There's more on that landing page but read this quick summary that really is as good as it sounds.
Akka site says: Actor Model
The Actor Model provides a higher level of abstraction for writing concurrent and distributed systems. It alleviates the developer from having to deal with explicit locking and thread management, making it easier to write correct concurrent and parallel systems.
There are some nice simple diagrams there that show how it works.
|
|
|
|
|
And what if it is not in .Net?
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
|
I'm guessing I've open-sourced something similar in C++.
All these concepts are used in the call servers that run in AT&T's wireless network.
|
|
|
|
|
Been there, done that. And had my 3D stuff running in the background. UI and the 3D engine scripted with XAML.
I have lived with several Zen masters - all of them were cats.
His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.
|
|
|
|
|
Yeah, I mean, I can do it real world, but it's even harder to do it simple enough that it can be used to instruct. That's part of what my rant was about.
Real programmers use butterflies
|
|
|
|
|
Instructing? I have done a bit of that, but you would not like my methods.
Edit: I think, some of my instructors would also have loved to send me to the ghost guard. Tenty times. (What Did You Say, Sergeant? - YouTube[^])
I have lived with several Zen masters - all of them were cats.
His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.
modified 15-Jul-20 18:35pm.
|
|
|
|
|
Do they involve beating the stupid out of students?
Real programmers use butterflies
|
|
|
|
|
Only once. The idiot started to turn around to me with a loaded machine pistol in his hands. Safety set to 'A', as in 'Amen'. I edited the last post. The little video is funnier than this story.
I have lived with several Zen masters - all of them were cats.
His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.
|
|
|
|
|
Yeah, that sounds wrong. One shared queue tends to work best.
|
|
|
|
|
I ended up changing it so it did it with one queue. I just lost in engineering tunnel vision for a bit, though eliminating the individual pipelines means I lose control over who the message goes to.
Real programmers use butterflies
|
|
|
|
|
|
Most of the time, but in this case, one thread is potentially multifunction meaning for example, you might care about which one you stop since one might be doing a job you're still interested in!
In the end I didn't go that route because it made everything too complicated for demonstration purposes.
Real programmers use butterflies
|
|
|
|
|
Quote: o as an instructional article I'm preparing code that can enqueue work items to a limited number of threads. If all the threads are busy and there's no more thread creation allowed (say you have a 3 thread limit) then one of the threads that's already busy enqueues the next message for when it's done with what it's currently processing. It schedules among the already busy threads using a round robin technique.
It's called a producer-consumer model and it's generally not so hard to handle. In most cases you need just one queue and all consumers feed from it. No rule that says a consumer cannot be a producer also. Consumers need to be polivalent meaning they know how handle any task that they pick from the queue.
If you have task-oriented consumers that know how to do only one task (sometimes called fussy-eaters) you're beter off with multiple queues and and each consumer waits in the appropriate queue.
Mircea
|
|
|
|
|
I figured out a better way to do it as I was coding it anyway.
Real programmers use butterflies
|
|
|
|
|
|
Personally I find multithreading to be quite interesting and I don't have problems with it at all. Multiprocessing systems are also amusing. Throw in multiple processes each with multiple threads and it's great fun.
Now I am working with CUDA and using literally thousands of threads. 2,304 on this laptop.
"They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"
|
|
|
|
|
I hope I didn't trigger this
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
|
Did you write all in less than 24 hours?
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
Yes
Real programmers use butterflies
|
|
|
|