Web Development

Windows Application, Processes & Threads

Posted by Marius Dornean on April 16, 2010  /   Posted in Technology, Web Development

How does Windows allocate CPU time?

CPU (Central Processing Unit) time is incredibly valuable. It determines how much and how fast code can be executed (in the context of length of time in regards to amount). Because of this, the operating system has to be conscious of how it split processing time between the different programs and services that are running. In order to make this a reality, various structures are needed to represents the executing code.

Applications – Processes – Threads

An application consists of one or more processes. A process is an executing program. One or more threads run in the context of the process. A thread is the basic unit to which the operating system allocates processor time. A thread can execute any part of the process code, including ?parts currently being executed by another thread. All threads in a process share the same memory and system resources. Each CPU core can execute one thread at a time, so multi core systems can execute as many threads in parallel at the same exact time as the amount of CPU cores available.

To recap, a threads make up a process which makes up an application. The simplest application will have one process consisting of one thread. Multi threaded applications can have multiple processes with multiple threads.

Thread Pools

Creating and terminating a thread is very time consuming. Memory allocation, thread management, and processing time are all involved in handling multiple threads. The more threads an application has, the more of these resources it will consume.

Some applications are written to take advantage of multiple threads and run processes (not windows process, but a literal one) in parallel. Some of these processes are very short lived and make the cost of creating and terminating the thread very expensive. In a way, the ROI is not high enough.

In order to make multi threading less expensive resource wise, thread pools were introduced. Thread pools are collections of worker threads that handle processes on behalf of the main thread.

A good analogy to this would be a restaurant. A new waiter (thread) would have to be hired every time a customer walked in the door. The waiter would take the order, bring the food, charge the fee, and do other tasks. When the waiter would be done serving that customer, they would be fired (thread terminated) and a new waiter hired for the next customer. This is incredibly inefficient if there are many customers and they spend very little time in the restaurant. A thread pool would be like having a staff of waiters ready to serve new customers as they walked in.

Process & Thread Priority

The operating system dictates how much processing time is given to each process based on the process priority. In windows, you can change the priority of a running process by using the Windows Task Manager and the processes tab. Similarly, processes can change the priority of the treads that make it up. By assigning a higher priority, that particular thread would get more processing time allocated to it.

A good use of this would be in an application that has heavy background processing. The thread responsible for the UI should have a higher priority then the thread which is running background processes. This would ensure that the application is responsive to the user input.

Memory Sharing

While multiple threads can execute the same exact code at the same time, they can not read and write to the same memory space. Safeguards are built in to ensure that memory access by multiple threads don’t overlap. Race conditions are one of the biggest problems in multi threaded applications. When one thread starts reading or writing to a piece of memory, other threads have to wait for it to finish.

An example problem would be two threads needing to read the same memory. Thread A starts reading some memory, but can’t finish until thread B sends a value back that thread A needs in order to finish its calculation. Thread B, in order to send the value back, needs to read the memory that Thread A has locked. Both threads are now waiting for each other and are in a deadlock.

Thread Timing

Windows splits up processing time between threads based on priority. As each thread gets a given amount of clock cycles to process. If the thread finishes processing before the time is up, it returns the processing time to the OS. The extra time is then added to the pool of time and split among the other threads. If after every thread has processed there is extra time left over, the time is allocated to the System Idle Process that can be seen with Windows Task Manager.

Multi Threading Considerations

Problems such as the deadlock and race conditions described are some of the challenges that face multi thread applications. Taking advantage of powerful systems with multiple cores capable of running multiple threads at once increases processing speed, but developers have a lot more of challenges to work with. The timing or threads, the work they do, and the memory they share are all thrown into the equation when designing multi threaded applications.

HAPPY CODING!

C# 4.0 – Optional Parameters, Default Values, and Named Parameters

Posted by Marius Dornean on March 22, 2010  /   Posted in Technology, Web Development

Parameters

Parameters are simply values that can be passed into a method. A method specifies what parameters, along with their type, it accepts and expects. Traditionally in C#, when we want to accept different sets of parameters or set default values, we would overload methods and supply the information by chaining the calls. This can be messy and can cause a lot of variations of a method. Keeping track of default values can also become a challenge.

static void Main(string[] args)
{
CreatePerson(“Marius”);
CreatePerson(“Marius”, 24);
CreatePerson(“Marius”, 24, “USA”);
}

static void CreatePerson(string Name)
{
CreatePerson(Name, 24, “USA”);
}

static void CreatePerson(string Name, int Age)
{
CreatePerson(Name, Age, “USA”);
}

static void CreatePerson(string Name, int Age, string Location)
{
//Logic…
}

Default Values & Optional Parameters in C# 4.0
In order to deal with the default value issue, C# 4.0 has introduced default values. By assigning a default to a parameter, we don’t require the value to be passed in which automatically makes it optional.

static void Main(string[] args)
{
CreatePerson(“Marius”);
CreatePerson(“Marius”, 24);
CreatePerson(“Marius”, 24, “USA”);
}

static void CreatePerson(string Name, int Age = 20, string Location= “USA”)
{
//Logic…
}

Named Parameters

When you have a method that has multiple optional parameters and you want to provide the value of only one, you have to use named parameters. These are quite simply parameters formatted as [Parameter Name]: Value

static void Main(string[] args)
{
CreatePerson(“Marius”, BirthPlace: “Other Location”);
CreatePerson(“Marius”, Age:26);
CreatePerson(“Marius”, BirthPlace: “Other Location”, Age: 26);
}

static void CreatePerson(string Name, int Age = 24, string BirthPlace = “USA”)
{
//Logic…
}
Visual Studio & Conditions

The only conditions placed on the developer is that optional parameters are to be declared at the end of the method arguments, after all of the full parameters have been declared. This means that when calling the methods, all full parameters must be passed in like a normal method, and then the named parameters can be given for the optional default parameters in any order.

Visual Studio 2010 adds supports for these new language features very nicely. As you can see below, the default values for the optional parameters are shown, and intellisense supports the selection of the named paramaters as expected.

VS 2010 Optional Parameters

Other Thoughts
As with each iteration of the C# language, Optional Parameters, Named Parameters, and Default Values give even more control to the C# developer. These new features will save a lot of overloading and method chaining, and will undoubtebly save a lot of developers from the “factory method” value instatiation.

HAPPY CODING!

^ Back to Top