I did an online session today celebrating the 25th anniversary of our Partner Infragistics. During the session, there were a lot of questions I was unable to answer because we ran out of time. Below are those questions and my responses (in italics). Many of these questions are answered by my various WintellectNOW videos. You can register for a free 14-day trial here.
- Are memory leaks reflected in the used memory statistic of the task manager?
No, Task Manager doesn’t offer the best column for this. Use PerfMon.exe and watch a Process’ Virtual Bytes.
- As a Windows OS advocate, I am curious on how Threading is implemented in Unix/Linux.
I answered this question on the call.
- Can a thread ever run (or be set to run) for longer than one quantum?
Can you increase the time quantum for long running tasks
Not directly. You can raise a thread’s priority so it prevents lower-priority threads from running.
- Can you point out the difference between threads and tasks?
A Task queues an operation to the thread pool. The thread pool then has one of its threads perform the operation. The thread pool threads are re-used over and over again to process all the queued operations. This reduces memory consumption and improves performance.
- Could you elaborate more on the logical vs. real CPUs?
A physical CPU can perform one operation at a time. But sometimes, the physical CPU must pause and wait for RAM to complete some work. This causes the CPU to sit idle. Hyper-threaded CPUs can execute another thread during these pause times to improve overall system performance.
- File Open Dialog was a common windows UI Control which exhibits this leak. Are there Windows APIs that tend to “leak” as well?
When you take a dependency on any technology (Microsoft or non-MS), you inherit its performance and efficiency problems. But, you saved yourself some time and energy. As a software developer you are charged with considering this tradeoff and determining if it is worth it for your application and your customers. Also, note that performance and efficiency are moving targets; that is, they change over time with later versions of the technology. With later versions things can get better or worse. So, when you take a dependency on some technology, these are the things you must be thinking about.
- Given the advantages of using the thread-pool wouldn’t it make sense to not allow the developer to create explicit threads and only provide access to “tasks” via the Win API?
Yes, in fact, the Windows Runtime (WinRT) API does not offer any functions allowing you to create threads; you MUST use the thread pool.
- How are threads from background processes scheduled?
I’m not sure how you exactly define a “background process.” But, for the most part Windows schedules threads in a round-robin fashion without regard to which process the threads are in.
- How could I make OS to schedule the most of CPU time working for an application?
This is dangerous thing to do and is discouraged. If the app goes into an infinite loop, then the rest of the system suffers greatly. However, you can raise the priority of threads in a process.
- How does a thread pool help and how many threads should a pool have?
Thread pools help because they create threads and re-use them over and over again. This saves time because they do not constantly create and destroy threads. In addition, the thread pool knows how many CPUs the PC has and tries to create 1 thread per core to reduce context switching; this also improves performance.
- How is the new Task class implemented? I had heard it was lighter weight than the Thread.
A Task is a small object in memory that knows how to queue a callback method to the thread pool. A task has no threads of its own. The task can monitor the lifetime of the queued item: did it complete? Did it thrown, did it return a value, etc.
- How to interpret the benefit of parallel computing (increased performance) related to this thread waste of resource?
Most PCs can easily handle the allocation of a few MBs of memory in order for you app to take advantage of parallel processing. If you just strive for no more than 1 CPU per logical core, then memory consumption will stay low and performance will stay high. - If a quantum is 30 ms – how long does the context switch take?
How much time does it take the OS to do a context switch (i.e. what is context switch overhead relative to the size of a quantum)?
The time for a context switch various based on many factors: CPU speed, CPU architecture, and so on. But, what makes the performance even worse is that the CPUs cache is usually invalid after a context switch causing a lot of cache misses when accessing RAM.
- If it is beneficial to create less thread as possible then what strategy should we choose when creating a WPF responsive Application? Any pattern or approach?
Typically UI apps (like WPF) have 1 GUI thread that process all user-interface events. Then, you queue up computationally-intensive work to the thread pool (via a Task) allowing the UI thread to respond to user input.
15. Jeff, even today we see some situations where we are not able to bring up task manager when the system is extremely busy.. why is this happening
It’s hard for to know for sure without being in front of the machine. But, my guess is that there may be some high-priority threads that are preventing Task Manager from displaying. This can sometimes happen with a bad device driver too.
16. Question set about duration of quantum, which you’ve indicated is 30ms – Has it always been 30 ms? What governs this duration? The HAL/clock interval?
Yes, the PC’s clock interval. There is a Win32 function that returns this info: GetSystemTimeAdjustment. Look at the lpTimeIncrement return value.
- So are threads automatically recycled after a certain period of not being used?
If the thread are thread pol threads, then yes. If they are not thread pol threads, then no.
- Threading topics apply to Azure programming?
Yes. Azure just creates virtual machines with Windows or other operating systems running in them. So information about threads apply to these VMs as well.
- ThreadPool.GetAvailableThreads() show the answer as 1024 instead of 8. How is that possible when the number of cores in my PC are just 8?
In the remarks section for this method, it says that it returns the “number of additional worker threads that can be started.” This says that the thread can create this menay threads but not that it has actually created them.
- What happens if you open another File Save As? Will more threads be orphaned?
No, it reuses the threads it created previously.
- What is difference between Foreground and Background thread.
The .NET CLR kills a process as soon as all its foreground threads have stopped running (instantly destroying any background threads). So, foreground threads keep a process running while background threads do not. - What is fiber support in case of threading
Fibers a light-weight threads with the OS kernel doesn’t know anything about. The developer must write code to “context switch” from one fiber to another. Each fiber does have its own user mode stack but all fibers (on the same thread) share a kernel mode stack. .NET doesn’t support fibers and probably never will.
23. What is the performance effect of having multiple CPUs with multiple cores vs a single CPU with an equivalent number of cores?
For the most part, the perf would be the same. Sometimes, CPUs have to communicate with each other (like when taking a thread synchronization lock) and this communication is faster if the CPUs don’t have to talk through the bus on the motherboard.
- How does async/await relate to threading?
These C# and VB language features allow you to perform I/O operations without blocking threads. This reduces the number of threads an application/service needs decreasing resource consumption and improving performance. I explain the value of all this in the video available here.
- Is it always 30ms? Even in Windows 8.1?
On all versions of Windows to date, the timer interrupt fires at the same rate.
- How do we get the additional threads started by File Open Dialogs (and other such controls) returned back to Thread Pool ?
You can’t control what a component does.
- So those logical processors have their own CPU cache too? Otherwise, won’t that impact performance?
The logical processors typically share the CPU’s cache. This is usually good as it allows bytes to be read once into the cache and shared by the other logical processors.
- Is it wasteful to switch contexts if all the threads on the system are waiting most of the time?
No. But, if a thread does not want to wait, then allowing it to run without context switching is faster than introducing context switching.
- Why is 32-bit arm about 1/2 of 32-bit x86 for overhead for memory for thread kernel object? Less data? More packing?
ARM CPUs have fewer CPU registers.
30. My question was: will there ever be such a concept supported by the .NET runtime, like the BEAM (Erlang virtual machine) threads, which said to be far more efficient and less resource expensive than threads, quicker context switch.
I’m not familiar with BEAM threads and what this actually means. I assume that they are like fibers and it is very unlikely that .NET will ever support fibers.
PUT AZURE TO WORK
We tackle the ongoing challenges of running in Azure.
We were one of the first Microsoft Cloud Solution Providers (CSPs), we are Cloud OS Network (COSN) certified with many production environments on the Cloud Platform System including Azure Certified for Hybrid deployments leveraging private and public clouds.
We know how to make Azure work for you.
Architected to
meet your needs.
We build solutions to address your individual business objectives with an eye to sustained technology innovation.
Deployed
flawlessly.
We manage the entire lifecycle from start to finish to ensure no surprises and allow you to focus on your business.
Operated reliably
24x7x365.
We only engineer and deploy environments which can be managed and maintained for you by our team of experts 24x7x365.
20 years of experience makes us a trusted partner you can count on.
We have developed a core methodology to ensure we accurately capture your needs and translate them into the best solution possible. This process gives you the peace of mind that your cloud investment will be aligned with the return you seek. We can be counted on to bring our industry experience and real-world best practices to operate Azure environments.
Our methodology involves 4 steps:
Assess > Migrate > Re-Platform > Operate
Moving into the cloud is not a one time event. With every customer engagement, we deliver a structured approach as follows:
Assess:
Rely on our team to map your existing environment to a corresponding Azure cloud.
Migrate:
Easily move from your existing environment to a public or private Azure cloud.
Re-platform:
Understand how to transform your applications to better take advantage of Azure capabilities.
Operate:
Our team actively manages all maintenance and optimization to keep your environment running at its best.
Azure Environments Managed by Atmosera
We help you determine how best to run your applications in the deployment best suited for their unique requirements.
24
Regions deployed
8
Regions coming soon
$15B
Invested by Microsoft
The Power of Microsoft’s
Hybrid Platform
Microsoft is the only hyperscaler to offer a true hybrid solution with the flexibility to run applications using private or public clouds — and the ability to move between them.
Atmosera has the skills and expertise necessary to help you architect, deploy and operate integrated hybrid cloud solutions based on Microsoft’s Azure stack and microservices.
Azure Certified for
Hybrid Cloud
Atmosera has developed core competencies and intellectual property to take full advantage of all Azure has to offer.
All Atmosera Azure services receive the “Azure Certified for Hybrid Cloud” designation which confirms it passed Microsoft’s rigorous validation process under the Cloud OS Network (COSN) program. Customers benefit from knowing that their solution was carefully planned and executed using best practices and proven methods.
Not Sure How to Get Started?
Atmosera Azure pre-configured solutions can help.
We find many customers are not sure how to get started.
We developed a series of pre-configured solutions around specific use cases common for many customers.
You can take advantage of thoroughly tested and optimized solutions and accelerate the return on your cloud investment.