Tuesday, April 8, 2008

[Programming] Threads (4) - synchronization

Threads (4) - synchronization

Previously we described the issue of joinable and detached thread scheduling. In practice, the situation is not that simple. Most of the threaded programs are concurrent programs, which means the operating system will run multiple threads at the same time. However, there's no way to know when a thread will be scheduled to run by the system. The order of thread's running is not deterministic.

Another big problem of threaded programming is that, unlike processes, threads share the same address space. This is one of the powerful aspects of threads - if one thread changes the value of a variable, other threads will see this modification subsequently. However, this sharing could also be dangerous without careful design and implemenation.

The third thing is that debugging a threaded program is not easy as debugging a single-thread program - you may not reproduce the same result in a multi-threaded program.

Therefore, the synchronization among threads is a very important issue. In order to understand the details of synchronization, let's first learn some new concepts one by one: critical section, atomic operation, race condition, mutex, semaphore, deadlock.

Critical section: it means a resource (usually implemented by a sequence of code) that should be exclusively used by a program for a period of its execution. In simple words, if a thread begins executing a critical section code, it must continue to the end without being canceled.

Race condition: since a critical section can only be exclusively used, when multi threads occur concurrently, they will compete for the critical section. In this situation, we say there is a race condition.

In order to eliminate race conditions and ensure a critical section being implemented successfully, we need a way to make operations atomic, which is described below.

Atomic operation: when we speak of an atomy, we usually mean that it is a tiny particle indivisible. So here the atomic operation has the similar meaning: once the operation starts, it will not be canceled, pasued or interrupted until it completes; at the meantime, no other operation will take place either.In multi-threaded programming, the operating system will provide and guarantee some operations to be atomic.

(to be continued)

Wednesday, April 2, 2008

[Wireless] China 3G phone (TD-SCDMA)

In the area of 3G, China proposed and submitted its own standard (TD-SCDMA) to ITU-R in 1998 and got it approved as one of the 3G standardized technologies in the world. After that, China has been working on this standard for about 10 years. In order to support this self-own standard, China government postponed the 3G licence from year to year, and China Mobile Group (the world's largest mobile phone operator ranked by number of subscribers, with over 376.38 million customers by the end of Feb, 2008) was selected to build pre-commercial networks for TD-SCDMA system. Many of my previous colleagues in China participated in this project.

Yesterday (Apr 1, 2008), a video clip about China 3G/TD-SCDMA phone was released in sina.com (the largest Chinese-language infotainment web portal). In this clip, a video call, a piece of stream media and some java supported applications are demonstrated on a TD-SCDMA phone (the model is U980 of ZTE). The video call looks not bad. Although UMTS (WCDMA), cdma2000 EVDO, and even HSDPA have been widely deployed by some service providers in the United states, as I know no one supports video call yet.

The Olympics game is coming soon. Although China said TD-SCDMA might not make Olympics deadline, it's still very possible that TD-SCDMA phones will be offered on hand and showcased at that time.