jump to navigation

Gmail classifies mail from Google as SPAM June 16, 2009

Posted by fahdk in Computing, Uncategorized.
Tags: ,
add a comment

Gmail-Spam-Bug

How to create multiplatform software using C/C++? May 23, 2008

Posted by fahdk in Computing.
Tags: , , , , ,
4 comments

I am sure there would be many ways and approaches to creating multiplatform software that people have used over the years. Here I humbly present three basic points about an approach that I have found to be useful. Readers are encouraged to voice their opinions in the Comments section.

Point 1: Start off with platform independent data types.

Basically create a header file that defines your own custom data types and maps them to the fundamental data types available on the system. Have separate sections for each platform and it is good to repeat your definition for each data type separately for each platform so that changing things on a particular platform would be easier later. Sample code follows…

#if defined (LINUX && __X86__)

typedef long long myint64;

#elif defined (LINUX && __X64__)

typedef long myint64;

#elif defined (SOLARIS && __X86__)

Point 2: Create a platform abstraction library.

Something like the Apache Portable Runtime or the Netscape Portable Runtime but instead of using these or developing something like these, you can develop your own small little static abstraction library and add only those abstractions to it that you need for your project. As a side note, remember that the Pthreads library is available on the windows platform as well.

Point 3: Create your software utilizing the platform independent data types and platform abstraction library.

Make sure you never use system defined types in your application nor do you use any platform specific system call directly.

This approach should help you create reliable and maintainable multiplatform software.

Thanks.

About Windows Events (synchronization) August 17, 2007

Posted by fahdk in Computing, Windows.
6 comments

Salaam.

I was trying to understand some C++ code on the Windows platform that uses the Windows Event synchronization object. Not being familiar with this object, I asked Shaikh Google about it but did not find an appropriate response.

Shaikh MSDN was also not very forthcoming about clarifying the issue, so I had to do a few experiments to figure it out myself. This blog entry sums up my understanding and should be helpful to those programmers who wish to know how the Windows Event object exactly works.

First we should discuss some terminology.

When an Event object is in signalled state, any thread issuing a Wait command on it will find the Wait successful (it will not block). In other words, signalled means unblocked.

When an Event object is in nonsignalled state, any thread issuing a Wait command on it will find the Wait blocking (assuming the timeout parameter for the wait was not 0). That means, nonsignalled can be thought of as blocked.

Now let us look at some APIs related to the Event object.

HANDLE WINAPI CreateEvent(
LPSECURITY_ATTRIBUTES lpEventAttributes,
BOOL bManualReset,
BOOL bInitialState,
LPCTSTR lpName
);

This call is used to create an Event object and returns a handle to it.
lpEventAttributes [in] is a pointer to a SECURITY_ATTRIBUTES structure. We will not go into the details of this parameter. It may be set to NULL.
bManualReset [in] is an interesting parameter and we will discuss this in more detail. If this parameter is TRUE, the function creates a manual-reset event object otherwise, it creates an auto-reset object.

Basically, Events in Windows can be of two types; manual-reset and auto-reset. A manual-reset Event object can be made to change its state from signalled to nonsignalled and vice-versa using the system calls SetEvent and ResetEvent respectively. This is fairly straightforward. To use this object, you will set it to nonsignalled (using ResetEvent) if you want the other thread to block on it, and you will set it to signalled (by calling SetEvent) if you want the other thread to proceed. As you can see, this is fairly simple.

Manual Reset Event (Pseudocode)

 

 

 

 

 

 

 

 

An auto-reset Event is not so straightforward initially. The idea behind this is that as soon as a thread unblocks on this object, the object will get blocked immediately and automatically for the other threads. So you do not need to call ResetEvent for this Event object. A Wait call on this object, when it succeeds, also does the ResetEvent (changing to nonsignalled) for you automatically. Notice that in this sense, it works like a semaphore with a max count of 1.

Auto Reset Event (Pseudocode)

 

 

 

 

 

 

 

 

lpName [in] The name of the event object. If this is NULL, the event object cannot be shared between processes. If it is not NULL, a global object will be created that can be shared between threads of different processes. Note that if an Event object was created previously by another thread, a handle to this object can be obtained using the OpenEvent API call.

Thanks.