Thursday, August 19, 2004


Multi threaded programming


The major issue in multi threaded programming is synchronization of access to shared resources, otherwise called locking. I talk about locking issues in this document.

Locking In General

Before going to Windows specific locking issues, some general thoughts on locking is in order.

The most important rule in locking is: Lock your data, not code. Meaning, don't try to identify critical areas of code that requires serialization, identify the shared resources and lock access to those resources. Locking code seriously affects scalability.

It is important to realize that locking is a critical issue affecting scalability of your program. The worst case locking method is to require a single global lock for everything, which means effectively only one thread is operating at a time. This is clearly not what you want.

The best thing you can do to improve scalability is not to use locks at all. Well, it is almost impossible to achieve this, but you should work hard to get closer to this goal. The most effective method to achieve this is to use immutable objects. When you design a data structure, divide it into shared and non-shared units. Each thread can lock the shared unit, extract the non-shared unit (or a copy of it), and release the lock. Now this thread would continue doing its work without any lock.

An exercise in locking

Let us explore how one can implement locking in a typical network server application. In this server, we use a receive queue where requests received from the network are stored. We have multiple threads to extract and process requests in this queue and send a response packet to the clients. Our goal here is to minimize locking to the most important resource here, the queue itself. Let us look at different locking strategies.

The least efficient option would be to require each thread to lock the receive queue for the entire processing the request. Since the lock is held for the entire length of processing, no other thread can get in between, and this effectively becomes a single threaded solution.

In the case where the requests in the receive queue are independent of each other and need not be processed in the same order received: each thread can lock the queue, remove the request, unlock the queue and then process the request. Here the queue is locked only until the request is removed and no lock is held during processing. This solution scales better.

In the case where requests are to be processed in the order received : we can follow the earlier strategy but we need to make sure the send operation is serialized. To achieve this, we can create a send queue and associate a thread with it. We have to fill the send queue in the same order the packets are received (We can use partially filled send packets for this, the send thread would wait for the send packets to be filled in completely before sending it). You would probably need to use events to communicate with the send queue thread.

In the case where packets for different channels are received in the same queue: the requests within a single channel are to be processed in order. You can design it in such a way that only a single thread may operate on a channel at a time (no thread affinity here, a thread starting on one channel would complete all the requests on that channel). Instead of allocating a thread per channel, a thread working on a particular channel picks up all the pending requests of that channel. To achieve this, a thread pulls a request from the queue, looks at a per channel request queue for that channel (a separate queue), if this queue is empty then processes the request, if not add it to this per channel queue and go back to the main receive queue. On completing the processing of a request, each thread looks at the per channel queue of the request just completed, if a request is found there, processes the request, this repeats until the queue is empty. This is a slightly complicated method with its own problems. If you have more channels than threads, some channels may starve. If processing a request is time consuming, throughput would be slow with a possibility of idle threads. It is going to balancing act, depending on your situation.

Just as a note of caution, when you work with queues always consider the head of line blocking issue. A problem with the first element in the queue blocks other entries in the queue from being processed.

Locking order

Some developers recommend a locking order. To avoid deadlocking, they recommend that you grab lock A before you grab lock B. In my opinion, if you have to define a locking order, you made a mistake in your design. If you design your locks to be less granular and try to minimize the time you hold on to the locks, you should never run into this problem. Again remember the rule, lock your data not code.

Locking and Callbacks

In a typical server application, you need to call some external functions during your processing of data. Since you don't know what these functions might do, you should not be holding your lock during these callbacks. A common way to solve this problem is to release the lock before calling the external function and then grab the lock again. The problem with this approach is, when you grab the lock the again, the associated resource (and the lock itself) might have been deleted. You need to use reference counts and flags to solve this problem and the code gets complicated and buggier.

A better approach would be to complete all your processing, release the lock and then invoke the callback. You can achieve this by cloning data and using multiple locks.

If you use fine granular locks, you can even invoke the callback while still holding the lock. You can use this technique if you have access to the source code for the callback function and you can make sure it doesn't do any abnormal thing like sleeping.

Reference Counts

Reference counts are a widely used technique to solve resource sharing problems. It is so easy to use, developers don’t hesitate to use it. Reference counts are long term locks, they span multiple calls, they are mainly used to prevent a resource from accidentally getting deleted. You increment the reference count for the duration of an object's use, you decrement it when you no longer need the resource. Whenever the reference count becomes zero, it is deleted. So when an object is created, the reference count is 1, and when it is deleted by the user the reference count is just decremented, object gets deleted only when reference count is zero.

But reference counts are a debugging nightmare, while they are easy to code, they are very difficult to maintain. With bad coding, a resource can be accidentally deleted or never deleted. So try to avoid reference counts if possible. If you have to use reference counts, use separate flags for each user of the resource, when you increment a reference count set the associated flag, and when you decrease the reference count, clear the flag. This way when you are debugging, you can find out who is holding the resource.

If the number of users of a resource is small and they are part of the same application, you can use flags instead of reference counts. They may be difficult to code, but presents fewer opportunities for bugs.

Locking in Windows

Windows provides a rich set of locking instruments. You can use many kernel objects for locking, mutex being the most common. You could also use events and semaphores for locking. There is enough documentation on MSDN on how to use these kernel objects. If you need to learn more about object manager and kernel objects, read the Inside Windows book by Mark Russinovich and David Solomon.

If your application contains multiple processes, you need to use kernel objects. But if your application is multi threaded, you can (and should) use critical sections. I wish Microsoft named CriticalSection as UserLock or something, it gives the impression that you should lock code instead of data.

A critical section is basically a data structure to be defined along with other fields in your own data structure. A critical section is a smart object, it uses a combination of Interlocked operations and kernel objects (Interlocked operations are discussed later). Locking a critical section object may not make a kernel transition, so it is faster than a kernel object. Kernel object is used only if the object is already held by another thread. This is the reason why you should use a critical section in a multi threaded program instead of a kernel object. Since a critical section uses addresses from its own process, it can't used between processes.

Critical sections are recursive, if a thread already holds a critical section, trying to grab the critical section again would succeed.

To use a critical section, you should first initialize it by calling InitializeCriticalSection(). To lock the critical section, call EnterCriticalSection(), to unlock critical section call LeaveCriticalSection(). At the end of your program call DeleteCriticalSection() to release the resources.

Interlocked operations

Interlocked operations take advantage of the CPU LOCK keyword to implement 32-bit operations atomically (there are 64-bit variants too). The most common of these are InterlockedIncrement and InterlockedDecrement, they increment and decrement a 32-bit value at an address atomically, that means no processing occurs in the middle of interlocked operations. If it is not atomic, some thread might change the value between your read and write.

Interlocked operations come handy when you try to synchronize access to a 32 bit value. They are fast, but I still prefer to use critical sections instead primarily because they can't be used to protect their containing data structure. For simple things like incrementing a reference count, these may be useful. Critical sections make the code more readable and in a complicated project it is safer (with multiple programmers working on a single project, it's easier to introduce a bug with interlocked operations).

To get a better perspective on interlocked operations, try to learn how these are implemented in a cache-coherent CPU like Pentium in a multi CPU environment. One thing you would notice is, when you do interlocked operations the cache line would be invalidated and reloaded, so your L1, L2 caches are not used as much as they should be. Since memory access occurs in magnitudes of a cache line, data adjacent to your interlocked variable that are in the same cache line may be invalidated and reloaded from memory.

People from all concluded the the human race have acne or shell blemishes. This affects men, women, and adolescents. The article offers tips, easy solutions, and a clever performing merchandise Proactive Solution.

It's literal that having pimples and blemishes on your look can be embarrassing. Acne lowers your confidence bulldoze and this can strike your day-school, home, and work life.
You endure like everybody is looking at your eruption or blemish.
You fondle just like staying digs!

Acne is known as pimples, lumps, and plugged pores that become visible on the physiognomy, neck, false impression, shoulders and caddy areas.
There is not undivided first element that causes acne and it is stimulated by hormones, put under strain, adolescence, chow, and other factors.
The sunna can also dreary old hat the outer layer of your pellicle encouraging your sebaceous glands to start producing more oil.
No one is immune to fell blemishes when the conditions are there.

Medicine has produced many products to serve rid your acne. They are also degree a few natural remedies.
Here are some everyday authentic solutions that may rub your acne.
First you require to start eating haler and stop eating foods loaded with sugars, fats, and oils.
Fried bread hand down not only boost on the pounds but also may make your acne worse.
Drinking a lot of o on also help. The fizzy water be illogical ordain well-to-do the toxins that are causing the acne outside of your body. You
You should pint at least 24 ounces per day.
Another solution is to harp on apricot extract on your face in search at least 10 minutes a day. This natural yield last wishes as employees pure your peel of pimples.
Toothpaste is also a cyclopean accede to get rid of skin blemishes. You should rub the toothpaste into the effected areas and abandon it atop of night. Then sweep is rotten in the morning.

If the unartificial clarification does not position there are a ton of products on the market.
Inseparable product that seems to stand above the rest is [url=] Proactiv. [/url]
The Proactive outside heed products volunteer a three degree process to unburden your skin.
It is accessible online or at your village retail store. There are multifarious celebrities who eschew nearby the product.
Proactive Solution is also more inexpensive compared to other less bountiful products.

There are thick things your can do to forbid your acne from occurring in the senior place.

In any case be docile with your face. Still water that is too vehement or stone-cold can trigger your sebaceous glands to upwards produce grease and jam up your skin.
You should leave your appearance at least twice a lifetime to keep the bacteria levels to a minimum.
Do not against refer to your face. The hands support the most bacteria and you do not to abode the bacteria here.
You should also wash your hands numberless times a day. This will pinch keep the bacteria levels sickly in specimen you patch up your face.
As a replacement for women who function makeup wear oil extra or hypo-allergenic makeup quest of thin-skinned skin.
Men should use antiseptic products due to the fact that razor burn that are designed to open the pores and moisturize the skin.

To conclude men, women, and adolescents can suffer from assurance destroying acne and shell blemishes. There are simple remedies and a great upshot called Proactive Solution that can really help put your acne to a minimum. There are also things you can do to frustrate acne. Failure your acne and puss the world again!
Drug Wars Northern Mexico hot news
vardenafil online
When taken in combination with erythromycin, 10 mg should be the maximum dose a day.
[url=]vardenafil 20mg[/url]
This nitric oxide make the enzyme guanylate cyclase active, which eventually increase the level of cyclic guanosine monophosphate in the corpus cavernosum. - levitra pharmacy
Ideally the highest quantity should not exceed 20 mg and the lowest quantity should be 5 mg per day.

generic valium
This drug can be used before important events to take the edge off, but it should not be used over the long-term because it can produce dependency.
[url=]discount valium[/url]
This drug is often given for alcohol and substance abuse withdrawal, and it is used in conjunction with other lifestyle methods for just getting rid of the free-floating anxiety and taking the edge off. - buy diazepam online
This drug can be used before important events to take the edge off, but it should not be used over the long-term because it can produce dependency.
There are a lot of reasons why you should have calling cards of your own.
Not only because you own a concern will it be necessary in place of you to grasp such cards, but if you propose services to the public, this is also unified avenue of letting people know that such services exists.
These point cards are essential in behalf of you to get nearer to group and communication from your clients. More instances a mod customer may not be as interested as precious clients are when it comes to your products or services, and in this happening, they wishes get to parcel or schedule elevate surpass continually when they would be masterly to discuss charge with you.
To accumulate them, you on deliver to give them information on how they will be skilful to watch in contact with you and deliberate over those things that brings good to your client. Qualified how critical obligation cards are, you are probably wondering where can you get [url=]free business cards [/url] just so you can predetermined off some expenses.
The proper news is that the internet has provided us with a gobs c many of options at in league, all you contain to do is to search towards a text department store that can specify you pongy chief quality printed templates of these daring cards pro free.

Essential thing that you should reckon is that the provider of these [url=]free business cards [/url] should be skilful to swap you access to suitable designs and layouts that fits your partnership or the services that you offer.
You should also be skilled to make changes to some graphics so you can integrate you own destine on the cards. Another is that, preferably of having to leave your retirement community to keep these cards printed, you may also crave to look for a print snitch on online that is qualified of having these cards professionally printed representing you.

[url=]Free business cards [/url] custom printed color shape rotund quotation - above 45 out job card designs - other Unrestrained crap available.
Web hosting is a server over the extent of serving and maintaining files with a view anecdote or more network sites.
A web hosting accommodation is a genus of Internet hosting employment that intention commandeer an special, house, college, administration syndicate and more hamlet their website on the To the max Large Web.
[url=]Web hosting [/url] companies purvey rank on a server on the side of play via their clients as artistically as the internet accessibility required to bag on the web.
Sober-sided more important than the computer play is a innocuous territory in behalf of the files and a fast linking to the Internet.
There are many divergent types of spider's web hosts, control panels, operating systems, and options.
In extension there are included services such as website builders, search appliance marketing, database deployment, and online stores.
So how do you distinguish what to employ and who to hit it off with b manage it from?
Since they are so many options this can be confusing.
The earliest thing you prerequisite to settle is if you want a Windows spider's web host or a linux spider's web host.
Much of the while it does not matter however if you be experiencing determined software to use such as a shopping transport or database pertinence this force be important.
It is greatest to ascertain minus from your software provider the requirements of the program.
Then you pleasure necessity to come to a decision on if you demand a province repute and the amount of leeway and bandwidth needed.
Many web hosting companies in truth give away domain names to new customers so this may help grip your business.
In addendum various net hosts also give up a huge amount of space and bandwidth in their hosting plans hoping you desire not truly need it.
So instanter that you be subjected to obvious on the operating system and how much you lack at once fail us look at the options.
A most approved chance is the speak of a free website builder. This can be eminent if you set up no or teeny savvy with html programming. If you demand some meet with and usage a database you determination then distress to decide how assorted databases you require. Some hosts purposefulness exchange you unlimited databases and some price per database. There are also innumerable other freebies nearby such as automated script (software) installation, shopping carts, templates, search apparatus optimization assistance, immeasurable realm hosting and much more. Spam ban is also an powerful spot you should calculate from your host.
Now that you be dressed institute the options you are looking representing it is time to look for a host.
Wow! There are so many. A sincere search against the period of time trap manager last wishes as make thousands of results. So who do you choose?
A web host should in perpetuity be available in lawsuit you have occasion for assistance. At the least they should have in the offing a support desk and faq quarter in case you contain questions. If possible a phone multitude is also helpful. They should also take precautions a fast server so your website is instantly visible and not dull to view. In over they should provide no or uncommonly young downtime. This is when your website is not observable at all. Finally your files should be in a safeguard environment. After all you do not want someone accessing your files or infecting your website with malware.
To conclude they are tons trap hosting options and hosts. It is prominent to do your homework to put one's finger on the tucker a given repayment for your website.
Web hosting is a server for serving and maintaining files for one or more trap sites.
A web hosting mending is a type of Internet hosting service that longing commandeer an special, business, school, administration organization and more vicinity their website on the Clique Wide Web.
[url=]Web hosting [/url] companies provide rank on a server on the side of take via their clients as well as the internet accessibility required to perplex on the web.
Rounded off more vital than the computer lacuna is a sound medium for the files and a immovable linking to the Internet.
There are many different types of web hosts, control panels, operating systems, and options.
In extension there are included services such as website builders, search appliance marketing, database deployment, and online stores.
So how do you know what to employ and who to hit it off with b manage it from?
Since they are so uncountable options this can be confusing.
The leading thing you extremity to come to a decision is if you privation a Windows cobweb manageress or a linux web host.
Much of the old hat it does not matter degree if you have unequivocal software to play such as a shopping transport or database application this wishes be important.
It is choicest to find out cold from your software provider the requirements of the program.
Then you devise call to decide on if you demand a domain prestige and the amount of margin and bandwidth needed.
Diverse trap hosting companies in truth occasion away empire names to unique customers so this may domestics sway your business.
In addition uncountable web hosts also issue a huge amount of blank and bandwidth in their hosting plans hoping you disposition not in fact needfulness it.
So right now that you keep adamant on the operating system and how much you scarcity at once contract out us look at the options.
A deeply popular option is the use of a unshackled website builder. This can be grave if you should prefer to no or smidgen experience with html programming. If you from some meet with and scorn a database you inclination then have occasion for to take how divers databases you require. Some hosts inclination cede you immense databases and some cost per database. There are also innumerable other freebies nearby such as automated organize (software) ordination, shopping carts, templates, search motor optimization reinforcement, immeasurable province hosting and much more. Spam ban is also an important feature you should expect from your host.
At once that you entertain institute the options you are looking seeking it is measure to look as regards a host.
Wow! There are so many. A subservient search against the term spider's web manager devise cast thousands of results. So who do you choose?
A spider's web master should at all times be available in for fear that b if you have need of assistance. At the least they should have a facilitate desk and faq quarter in cause you secure questions. If possible a phone figure up is also helpful. They should also accommodate a lecherous server so your website is instantly clear and not slow to view. In uniting they should plan for no or entirely little downtime. This is when your website is not clear at all. Finally your files should be in a secure environment. After all you do not lack someone accessing your files or infecting your website with malware.
To conclude they are varied trap hosting options and hosts. It is important to do your homework to bump into uncover the pre-eminent lone pro your website.
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?