Reentrant and thread safe functions

December 31st, 2009 by Kevin | 1 Comment | Filed in programming

Threads

Process with two concurrent threads

©en:User:Cburnett

A thread is a sequence of execution in a process. Multiple threads can run concurrently in a process as shown in the figure above. The feature of concurrently executing threads of a process is that they share resources such as memory.

An example of a project using threads that I have worked on is playing an MPEG4 stream. The stream contains both audio and video tracks. We should be able to play them concurrently with the timing information provided inside the tracks. So we create a thread to play the audio, another thread to play the video and a third thread to maintain synchronization between the two.

Since the threads share the same address space, we can share the synchronization information between them. The only issue is when the address space is accessed concurrently and the threads are writing on it at the same time or with one read and the other write. To solve this problem we have to make the functions thread safe.

The way to make functions thread safe is to use one of the following mechanisms:

1) Using local variables: By ensuring that we just use local variables we can make the function thread safe. This is because even though the global address space is shared between the threads, each thread is assigned it’s own stack with separate memory for it’s local variables.

2) Using mutual exclusion: Mutex as they are popularly know are another way of making a function thread safe. This is done by ensuring that the shared resources of the function are accessed by only one thread at a time i.e the access to these critical shared resources are mutually exclusive among the threads.

pthread_mutex_t VideoStatusMutex = PTHREAD_MUTEX_INITIALIZER;

static int GetVideoStatus()
{
  int tmp = 0;
  pthread_mutex_lock(&VideoStatusMutex);
  tmp = g_VideoStatus;
  pthread_mutex_unlock(&VideoStatusMutex);
  return tmp;
}

static int SetVideoStatus(int VideoStatus)
{
  pthread_mutex_lock(&VideoStatusMutex);
  g_VideoStatus = VideoStatus;
  pthread_mutex_unlock(&VideoStatusMutex);
  return 0;
}

Whenever we need to use a share resource like g_VideoStatus, we use the mutex functions SetVideoStatus() and GetVideoStatus() instead.

3) Reentrant functions: To make a function a list of requirements is below (from Wikipedia):

  • Must hold no static (or global) non-constant data.
  • Must not return the address to static (or global) non-constant data.
  • Must work only on the data provided to it by the caller.
  • Must not rely on locks to singleton resources.
  • Must not modify its own code. (unless executing in its own unique thread storage)
  • Must not call non-reentrant computer programs or routines.

All reentrant functions are thread safe but not all thread safe functions are reentrant. An example of a reentrant function is our recursive factorial example which satisfies all the above conditions.

int fact(int n)
{
   if(n == 0 || n == 1)
   {
      return 1;
   }else{
      return n * fact(n-1);
   }
}
 

Related Posts:

Tags: ,

What skills should a good developer possess?

December 28th, 2009 by Kevin | 6 Comments | Filed in development

The capability matrix below provides an excellent capability matrix of what skills a good software developer should possess.

Capability Matrix

Skill Software Engineer (2+ yrs) Sr Software Engineer (4+ yrs) Tech Lead (8+ yrs) GM (10+ yrs)
Algorithmic skills
Understand and dissect complex problems quickly Must have Must have Must have Must have
Understand trade-offs between space / time complexity Must have Must have Must have Must have
Come up with solutions with minimal space / time complexity Should have Must have Must have Must have
Reasonably mathematically savvy Bonus Bonus Should have Should have
Familiarity with graph theory, graph traversals etc Bonus Bonus Should have Should have
Data Structures
Basic Knowledge of data structures – Hashmaps, Binary tree, B-Tree, B+Tree, Linked Lists etc Should have Must have Must have Must have
Understanding of trade-offs between various data structures etc Should have Must have Must have Must have
Advanced Knowledge of implementation of data structures – Hashmaps, Binary tree, B-Tree, B+Tree, Linked Lists etc Should have Should have Must have Must have
RDBMS
Basic RDBMS knowledge Must have Must have Must have Must have
Advanced RDBMS knowledge Should have Should have Must have Must have
Query optimization Should have Should have Must have Must have
RDBMS tuning Should have Should have Must have Must have
Replication and Clustering Should have Should have Must have Must have
RDBMS Scalability Should have Should have Must have Must have
Caching
Basic knowledge of caching Should have Must have Must have Must have
Advanced caching strategies Should have Should have Must have Must have
Design and modeling
Strong knowledge of OOPs Must have Must have Must have Must have
Design patterns and application thereof Should have Must have Must have Must have
Understanding of KISS, YAGNI, DRY, SOC, SRP, Loose Coupling etc Should have Must have Must have Must have
Networking
Basic knowledge of DNS Should have Must have Must have Must have
Protocol level understanding of TCP / UDP Should have Should have Must have Must have
Deep understanding of OSI stack Should have Should have Should have Must have
Basic understanding of Routing concepts Should have Should have Should have Must have
Socket programming
Ability to implement a protocol server/client Should have Must have Must have Must have
Ability to write high performance server/client Should have Should have Must have Must have
Understanding of Async I/O Should have Should have Must have Must have
Understanding of various network protocols Should have Should have Should have Must have
Web application development
Basic HTML and CSS Must have Must have Must have Must have
Basic Javascript Should have Must have Must have Must have
Protocol level knowledge of HTTP Should have Must have Must have Must have
Advanced HTML and CSS Should have Should have Must have Must have
Advanced Javascript Should have Should have Must have Must have
Ajax / Flex Should have Must have Must have Must have
Understanding of MVC architecture Should have Must have Must have Must have
Multi-threading and concurrency
Basic knowledge of multi-threading Must have Must have Must have Must have
Advanced knowledge of multi-threading / trade-offs Should have Must have Must have Must have
Automated Testing
Understanding of unit testing Should have Must have Must have Must have
TDD Should have Must have Must have Must have
Writing code conducive to Automated testing Should have Must have Must have Must have
Planning and writing functional tests Should have Must have Must have Must have
Planning and writing stress tests Should have Should have Must have Must have
Knowledge of unit and functional testing frameworks for relevant platforms Should have Should have Must have Must have
OS Concepts
Understanding of OS concepts, kernel, interrupts, native libraries etc Bonus Bonus Should have Must have
Understanding of OS process scheduling concepts Bonus Bonus Should have Must have
IPC / RPC / Web Services
Knowledge of different forms of IPC / RPC Should have Must have Must have Must have
Knowledge of various protocols involved and trade-offs Should have Should have Must have Must have
Basic implementation knowledge of any one RPC mechanism Should have Must have Must have Must have
Advanced knowledge of SOAP / REST / other remoting Should have Must have Must have Must have
Security
Knowledge of OWASP Bonus Should have Must have Must have
Knowledge of Network layer security Bonus Bonus Must have Must have
Knowledge of secure architectures Bonus Bonus Should have Must have
Internationalization
Knowledge of Unicode and its implementations Bonus Should have Must have Must have
Knowledge of implementation of internationalized interfaces Bonus Should have Must have Must have
Understand implications of internationalized data in RDBMS, searches, indexing etc Bonus Should have Must have Must have
Development Methodologies
Knowledge of Agile, XP, Scrum, TDD and pairing Bonus Should have Must have Must have
Knowledge of Identifying code smells and Refactoring Should have Must have Must have Must have
Scaling
Write and plan stress tests to determine scalability Bonus Bonus Must have Must have
Ability to identify scalability and performance bottlenecks quickly Bonus Bonus Must have Must have
Ability to determine whether an application is / will be disk bound, memory bound, cpu bound, network bound etc Bonus Bonus Must have Must have
Understanding of all layers from the hardware to the application to determine bottlenecks Bonus Bonus Must have Must have
Knowledge of scaling techniques on the application side such as Async IO, caching, multi-threading etc Bonus Bonus Must have Must have
Knowledge of scaling techniques on the data side such as Identifying optimized data structures, caching strategies, Horizontal / Vertical partitioning, replication / clustering Bonus Bonus Must have Must have
Knowledge of scaling techniques on the app server side such as clustering and load balancing Bonus Bonus Must have Must have
System Administration
Basic Unix commands and shell operation (including grep, awk, sed, regex and shell / perl scripting) Bonus Should have Must have Must have
Basic Windows administration Bonus Should have Must have Must have
Usability
Knowledge of information architecture Bonus Should have Must have Must have
Ability to design intuitive UI Bonus Should have Must have Must have
Communication Skills
Good grammar – written and oral Must have Must have Must have Must have
Ability to understand discussions well Must have Must have Must have Must have
Ability to articulate a concept / subject well Must have Must have Must have Must have
Good command over vocabulary Should have Should have Must have Must have
Penchant for Writing
Frequently writes internal / external documents / articles Should have Must have Must have Must have
Conducts internal and external training sessions Should have Should have Must have Must have
Penchant for reading
Avid reader of tech and industry blogs, articles, news and books etc Must have Must have Must have Must have
Strategy and Vision
Business acumen Bonus Bonus Must have Must have
Ability to determine features and product strategy Bonus Bonus Must have Must have
Platform Selection
Ability to select languages, OS, RDBMS, Data structures, Frameworks, libraries, Hardware for an application Bonus Bonus Must have Must have
Understand performance, cost and other trade-offs between myriad platform choices Bonus Bonus Must have Must have
Architecture
Understanding of deployment architectures and their trade-offs Bonus Bonus Must have Must have
Capacity Planning
Ability to plan and conduct capacity planning exercises Bonus Bonus Must have Must have
Soft Skills
Likeable / people’s person Should have Must have Must have Must have
Ability to garner respect amongst peers by demonstrable knowledge and actions Should have Must have Must have Must have
Proactive mentor who loves to impart and share knowledge Should have Must have Must have Must have
Attract and Recruit ‘A’ players Bonus Bonus Must have Must have
Train and grow team Bonus Bonus Must have Must have
Contribute to process evolution Bonus Bonus Must have Must have
Motivate and inspire the team Bonus Bonus Must have Must have
Product Evangelism
Frequently blog Bonus Should have Must have Must have
Misc
Humility Must have Must have Must have Must have
Directi / CC BY-SA 3.0

While it might not be possible to possess all the skills (though I am sure there may be some who could boast of that level), it gives a good direction towards becoming a successful software developer. Perhaps every budding developer should be given a copy of this matrix. And perhaps every organization who wants to achieve success should be looking forward to hiring people who possess many of these capabilities.

I am quite impressed with this list and would be trying to achieve as much as possible in the coming year. Perhaps you could consider it my new year’s resolution to become a much better software developer. The points in blue are the ones that I have knowledge about or am currently working on. So it looks like a long, long way to go :)

[Updated] Another interesting chart showing Programmer Competency Matrix.

 

Related Posts:

Tags:

Your favorite = Free Google Wave Invite

December 27th, 2009 by Kevin | No Comments | Filed in General

Google Wave is an online communication and collaboration tool that makes real-time interactions more seamless — in one place, you can communicate and collaborate using richly formatted text, photos, videos, maps, and more.

Right now google wave is available by invitations only. I have twenty invitations left which I want to give away. All you have to do is mention your favorite programming blog/website in the comment and why it is so.

Invitations on a first come first serve basis. Please note that it may take some time for your google wave account to be activated.

 

Related Posts:

  • No Related Posts

Tags: