MyThreadPool | A simple threadpool implemented by C
kandi X-RAY | MyThreadPool Summary
kandi X-RAY | MyThreadPool Summary
A simple threadpool implemented by C#.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of MyThreadPool
MyThreadPool Key Features
MyThreadPool Examples and Code Snippets
Community Discussions
Trending Discussions on MyThreadPool
QUESTION
I know TTask
and have used TTask.WaitForAll(array)
successfully, as well as TParallel.&For()
.
But now I want to do a seemingly simple thing and don't find out how:
I have an unknown number of items coming in, this can be millions or only a few, and I don't know in advance. How can I work on them in parallel (just about 4 threads or so), but without a queue? If max threads are already busy, I want to wait for the next free slot. Something like a TTask.Run()
which just doesn't come back until it really starts running.
I guess I'm just overseeing something simple...?
When I'm through, I want to wait for all remaining tasks to finish. But of course I don't want to have millions of them in an array for WaitForAll()
.
I can imagine a possible solution (but I don't like it and hope for a much easier one using TTask
or similar):
- Push the work to a
TThreadedQueue
, it would automatically let me wait if the queue is full - Start 4 threads and let them pop from that queue in a loop
I know this might be the preferred way anyway in some cases, but my situation would not profit from it (like reusing any objects, connections or so).
Pseudocode of what would be nice and clean:
...ANSWER
Answered 2021-Feb-03 at 12:01This seems to be a working solution, but abusing TParallel.&For
is maybe not really nice. I'm still hoping for a better answer.
QUESTION
I am starting with threads and wrote for the sake of learning the following simple program, which later would be used to calculate about 100,000 times a formula (it is a relatively simple one but which takes an iterated range of values).
The problem with it is that I expected every thread to execute in almost no time and thus the complete program to finish nearly immediately, but the fact is that everything runs too slow (about 10s)...
...ANSWER
Answered 2020-Jan-16 at 08:30The problem with it is that I expected every thread to execute in almost no time
Right. You're ignoring the fact that creating a new thread is a relatively expensive operation. Far, far more expensive than "acquiring a lock and incrementing an integer" which is the work you're doing in the thread.
To give a real world comparison, it's a little like ordering a new car, waiting it to be delivered, and then driving it 1km. That's going to be slower than just walking 1km.
Using the thread pool would be faster, but you're not using it correctly - you're launching one thread pool task which then creates all the other threads again.
I would encourage you to look at using Task
instead, which normally uses the thread pool under the hood, and is a generally more modern abstraction for this sort of work.
QUESTION
I am using QThreadPool
to run tasks in my application in parallel. The tasks take the thread pool as an argument, so they can start new tasks. How can I write unit tests for the tasks and assert that the correct next tasks are started?
ANSWER
Answered 2019-Mar-25 at 16:44Regarding the problems about the fact that QThreadPool::start()
is not a virtual function, you can do something like this:
Instead of overriding a function, you can use your subclass in MyTask and use a different function that will call run. Something like this:
QUESTION
I have the following code:
...ANSWER
Answered 2017-Sep-25 at 05:17I don't know exactly your case, but easiest way - use OkHttp API to configure concurrency level, for example, this is default concurrency strategy of OkHttp
But you can have own strategy if you set own Dispatcher
instance to OkHttpClient.Builder
Of course, you can use also coroutines
Your current implementation is incorrect because you create coroutines dispatcher for each item, but to have shared pool of threads all the coroutines should use the same dispatcher, just move newFixedThreadPoolContext
creation outside of the loop (now you have 1000 dispatchers each with 10 threads).
But I don't recommend you to use coroutines + blocking calls, better to configure OkHttp concurrency (it's more flexible) and use coroutines with non-blocking calls (you can write own adapter or use an existing library like kotlin-coroutines-retrofit). It will allow you to mix your http requests and UI code or other tasks.
So if you use non-blocking API + OkHttp internal concurrency, you don't need to have special code to control concurrency, of course, you can limit the number of concurrent calls like in your example above (with fixed dispatcher construction), but I really don't think that it makes much sense, because you can decrease concurrency level, not increase it.
After moving to non-blocking API you can just run all your coroutines in any coroutines dispatcher in parallel (even in UI thread) and wait for results without blocking.
Also, implicit control of concurrency using OkHttpClient configuration looks as a more right way in terms of architecture (you can have DI code that configures Retrofit + OkHttp and provides it to your client code with preconfigured concurrency policy). Of course, you can achieve that using other approaches, but this one looks more natural for me.
QUESTION
I am trying to write a Custom Thread Pool object to run a certain function that might take long time.
I have written the following to code to run a simulation:
...ANSWER
Answered 2017-May-26 at 10:17You are calling OnThreadDone
immediately after creating the thread, without waiting for it to complete:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install MyThreadPool
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page