threadpool

Is Task.Factory.StartNew() guaranteed to create at least one new thread?

笑着哭i 提交于 2019-12-20 03:52:05
问题 I understand that the TPL does not necessarily create a new thread for every task in a parallel set, but does it always create at least one? eg: private void MyFunc() { Task.Factory.StartNew(() => { //do something that takes a while }); DoSomethingTimely(); //is this line guaranteed to be hit immediately? } EDIT : To clarify: Yes, I mean is it guaranteed that the thread executing MyFunc() is not going to be used to execute //do something that takes a while . 回答1: It depends on what you mean

Using threadpools/threading for reading large txt files?

戏子无情 提交于 2019-12-20 01:59:23
问题 On a previous question of mine I posted: I have to read several very large txt files and have to either use multiple threads or a single thread to do so depending on user input. Say I have a main method that gets user input, and the user requests a single thread and wants to process 20 txt files for that thread. How would I accomplish this? Note that the below isn't my code or its setup but just what the "idea" is. Example: int numFiles = 20; int threads = 1; String[] list = new String[20];

NotSupportedException on WaitHandle.WaitAll

孤人 提交于 2019-12-19 21:59:28
问题 I am trying to execute the following code. The code tries to parallely download and save images. I pass a list of images to be downloaded. I wrote this in C# 3.0 and compiled it using .NET Framework 4 (VS.NET express edition). The WaitAll operation is resulting in a NotSupportedException (WaitAlll for multiple handles on a STA thread is not supported) everytime I try to run my program. I tried removing SetMaxThreads , but that didn't do any difference. public static void SpawnThreads(List

Thread Pool and .IsBackground in .NET

99封情书 提交于 2019-12-19 17:28:21
问题 MSDN, as well as many other sources, claim that worker threads in the thread pool are always background. "Thread pool threads are background threads." (MSDN) "Pooled threads are always background threads." (Threading in C#, Joseph Albahari) I can easily make the worker thread non-background by setting Thread.CurrentThread.IsBackground = false; And the application will be waiting until the thread finishes. What's wrong with that? 回答1: When does the thread finish? When your method ends? I

C#: Asynchronous delegates vs ThreadPool.QueueUserWorkItem when initiating many connections

 ̄綄美尐妖づ 提交于 2019-12-19 10:49:19
问题 I have to send many web service calls out to delete a bunch of records in Amazon SDB (unfortunately, rows can only be deleted one at at time currently). I am using Amazon's SDB c# library which does not use async WebRequests. Currently I use ThreadPool.QueueUserWorkItem to queue up a bunch of calls (I configured my connectionManagement maxconnection to allow for a bunch of connections) this works well..as the request is sent out, it blocks and another request is made. Is this the wrong way to

Multithreading best practices : constraining tasks newFixedThreadPool

无人久伴 提交于 2019-12-19 10:42:40
问题 I want to launch a lot of tasks to run on a database of +-42Mio records. I want to run this in batches of 5000 records/time (results in 850 tasks). I also want to limit the number of threads (to 16) java starts to do this for me and I am using the current code to accomplish this task: ExecutorService executorService = Executors.newFixedThreadPool(16); for (int j = 1; j < 900 + 1; j++) { int start = (j - 1) * 5000; int stop = (j) * 5000- 1; FetcherRunner runner = new FetcherRunner(routes,

Using Task.Yield to overcome ThreadPool starvation while implementing producer/consumer pattern

社会主义新天地 提交于 2019-12-19 08:38:06
问题 Answering the question: Task.Yield - real usages? I proposed to use Task.Yield allowing a pool thread to be reused by other tasks. In such pattern: CancellationTokenSource cts; void Start() { cts = new CancellationTokenSource(); // run async operation var task = Task.Run(() => SomeWork(cts.Token), cts.Token); // wait for completion // after the completion handle the result/ cancellation/ errors } async Task<int> SomeWork(CancellationToken cancellationToken) { int result = 0; bool loopAgain =

How Can I determine the number of items in ThreadPool Queue

故事扮演 提交于 2019-12-19 06:26:06
问题 I am using the ThreadPool to queue 1000's of workitems While(reading in data for processing) { args = some data that has been read; ThreadPool.QueueUserWorkItem(new WaitCallback(threadRunner), args); } This is working very well, however, as the main thread queues the requests faster than they are processed memory is slowly eaten up. I would like to do something akin to the following to throttle the queueing as the queue grows Thread.Sleep(numberOfItemsCurrentlyQueued); This would result in

Ruby: Any gems for threadpooling?

[亡魂溺海] 提交于 2019-12-19 04:14:14
问题 Is there a gem for threadpooling anyone can recommend? 回答1: From my experience forking/process pooling is much more effective than thereadpooling in Ruby (assuming you do not need much in terms of thread communication). Some time ago I created a gem called process_pool, which is a very basic process pool with a file based job queue (you can check it out here: http://github.com/psyho/process_pool). 回答2: I would try https://github.com/ruby-concurrency/concurrent-ruby/ . It's basically a port of

(How) Does TPL use (CLR) Thread Pool?

半世苍凉 提交于 2019-12-18 15:34:09
问题 I am currently researching Task Parallel Library and I read somewhere that TPL actually uses thread pool mechanism from CLR-Level. I couldn't find any article confirming this information. I know, TPL has task queues for each thread and uses some special work-stealing algorithm for balancing. As far as I know, it creates one thread for each processor. Thread pools started to use task objects of TPL since .NET 4. I can not understand how TPL uses the thread pool. Thread-Pool pattern states, the