Showing posts with label Thread. Show all posts
Showing posts with label Thread. Show all posts

Monday, August 10, 2009

Chapter 16-Lesson 1-2

  • System.Globalization namespace is required for creating culture-sensitive applications.
  • As a rule, a culture will be grouped into one of three categories: an invariant culture, a neutral culture, or a specific culture.
  • Thread.CurrentThread.CurrentCulture gives a CultureInfo object representing the culture of the system in which the thread is executing.
  • We should use formatting options when setting/displaying strings in our applications so that any changes made to the CurrentCulture would propagate to it.
  • CurrentUICulture represents the culture for displaying the application and may or may not be the same as CurrentCulture.
  • Also, CurrentUICulture must be set at application startup (suitably in Main( ) or Form constructor) which is a behavious opposed to CurrentCulture property. (which may be set at any time.
  • The DateTimeFormatInfo class provides a comprehensive set of methods and properties to handle and respond to the dates of different cultures.
  • Thread.CurrentThread.CurrentCulture.DateTimeFormat property is used for this purpose. It is of type DateFormatInfo.
  • NumberFormat property represents the same case for numbers and currencies.
  • CompareInfo property of CultureInfo allows for culture sensitive comparisons (usually for strings)
  • CultureRegionAndInfoBuilder class is new in .NET 2.0
  • A reference to C:\Windows\.NET Framework\[VERSION]\sysglobl.dll is required to use this class.
  • This class takes CultureAndRegionModifiers enumeration value in its second argument for constructor which specifies how much of an existing culture should be loaded in the new custom culture.

Tuesday, July 14, 2009

Chapter 7-Lesson 2

  • There are three operations usually involved with arithmetic operations on variables, i.e. Load Variable value into Register, Change value in Register, Write Back the new value to variable.
  • In threaded code, all the above steps/operations are treated as atomic operations.
  • Therefore, it might happen that two threads read/load the same variable into the register, change its value equally and write it back, all at the same time. (Valid in multi-core or multi-processor scenario)
  • Interlocked class may be used to accertain that a certain variable is accessed by only one thread in a multi-threaded application.
  • Interlocked class has static methods which can be used when performing arithmetic operations on variables.
  • However, interlocked class has some limitations. It cannot be used for a wide variety of types and cannot be used to make pieces of code thread-safe. (Refer to Page 392)
  • To declare a piece of code as thread-safe, use lock keyword to create thread-safe block. Lock keyword requires and object reference passed to it (usually this).
  • This creation of lock block is called Synchronization Locks.
  • Synchronization Locks may also be created using Monitor class static methods.
  • To avoid deadlocks, we may use Monitor.TryEnter( ) method. Otherwise use Timeout.Infinite static property to tell the thread to wait as long as it takes.
  • If we wish to deal (synchronize) reading and writing operations seperately, we may use ReaderWriterLock class to lock certain data only for reading or writing.
  • LockCookie type object would be required to downgrade a WriterLock to a ReaderLock. (This implies that a WriterLock is downgradable only if it was previously upgraded from a ReaderLock)
  • Three OS kernel level objects are represented in .NET by classes named Mutex, Semaphore, AutoResetEvent and ManualResetEvent. All of these derive from WaitHandle class.
  • Mutex is almost same as Monitor class with the fact that it allows synchronization across AppDomain and process boundaries.

Chapter 7-Lesson 1

  • Thread class is used to create and start a thread.
  • ManagedThreadId property may not be the same as operating system thread ID.
  • Abort( ) methods actually raises the ThreadAbortException to kill the thread.
  • Resume( ) and Suspend( ) methods have been deprecated.
  • In order to notify the thread host about the critical region of a thread, use static methods BeginCriticalRegion( ) and EndCriticalRegion( ) of Thread class. Use these methods inside a method that would be used as a thread.
  • The static Sleep( ) method blocks the current thread for a certain amount of time during which other threads may run.
  • SpinWait( ) does the same but does not allow other thread to run during that time.
  • Join( ) method of Thread class asks the host thread to wait until the child/worker thread has finished executing.
  • ParameterizedThreadStart delegate is used to represent threads that require parameters/arguments for calling.
  • Page 383*