Review: "HTML 4 & 5: The Complete Reference" By O'Reilly Media for iOS

This is not a book stuffed into an app. It is an application. The second thing to know is that the content is very good.

The content is a refresh of the O’Reilly HTML & XHTML Pocket Reference, Fourth Edition and benefits from have been honed over multiple editions. As a pocket reference it shows a decision to opt for concision over being exhaustively deep. But it is good because it is on point.

The content can be browsed or searched. Browsing includes a list of elements and a list of attributes. From the attribute list attribute entries link back to element entries for the elements that use the given attribute.

The search feature works across both elements and attributes but apparently there’s no word stemming. Searching on ‘sel’ finds no results. ‘select’ finds results that include the select element but not the selected attribute.

The app was developed in HTML5, CSS3, and jQTouch. PhoneGap was used to create an iOS executable. There are plans to bring the app to Android so using PhoneGap probably seemed reasonable. But there are quirks present that probably result from not being directly developed to iOS. Overall I didn’t see performance issues on a first gen iPad and on an iPhone4 but scrolling and scrubbing in the elements and attributes lists was sometimes touchy. What seemed like a light flick would sometimes send the list flying to the end. Touching the scrub bar is not always recognized, so instead of scrubbing through alphabetically the list is only scrolling. There’s no visual cue when the scrub bar has been activated.

Despite a few deficiencies it’s a huge win in convenience and utility to have this content in the form of an application. I’ve been working on updating an older site and this application has proved its worth as what it claims to be: a complete reference.

HTML 4 & 5: The Complete Reference is on the iTunes App Store.

Thanks to O’Reilly for providing the application for review.

Defensive Threading

Designing and writing multi-threaded code is hard. Threading adds a debilitating amount of complexity to the Von Neumann architecture and, despite 50+ years of theory and implementation, the threading support in most programming languages is low level, the equivalent of malloc rather than GC managed memory.

If you’re a developer and you approach threading with trepidation then here are some experience tested guidelines and techniques that I try to follow. The overall strategy is to reduce the surface area for bugs and to increase the maintainability of the code. These are not hard and fast rules. It’s important to be pragmatic and to adjust for the specific situation.

But first, please note that I’m no expert. I’m sharing what I’ve learned and I know I’m not done learning. If this post helps you, I want to know. If you think I’ve got something wrong, I want to know. Thank you.

 

Avoid sharing data.

Sharing data between threads is the most common source of threading bugs. By definition many of the nastiest threading bugs can only occur when data is shared. Safely sharing data requires synchronization either in the form of a lock or an atomic primitive. Getting the synchronization right is not always easy. Avoid sharing data and the threading bug potential goes way down. As a bonus, the code can potentially run faster because the possibility of synchronization contention is eliminated.

It may seem that without sharing data, threads are pretty useless. Not so. But this is a case where you need to be willing and able to trade space for speed and safety. Give a thread it’s own private copy of everything it may need, let the thread run to completion (‘wait’ or ‘join’ on the thread), and then collect the results of it’s computations.

Avoid sharing logging.

Logging is data. See above. Sometimes the messages generated on a thread need to be reported more or less immediately. But if a delay can be tolerated, a thread can have it’s own private log that gets collected after the thread has run to completion.

When you must share, prefer lighter weight synchronization mechanisms.

I didn’t understand lock-free threading until I understood that lock-free doesn’t mean no synchronization mechanisms at all.

Under Windows the Interlocked* APIs represent atomic operations implemented by specific processor instructions. Critical sections are implemented via the interlocked functions. The interlocked functions and the critical section on Windows and the equivalent on other platforms are generally the lightest weigh synchronization mechanisms.

Technically the interlocked functions are not locks, they are hardware implemented primitives. But colloquially developers will speak of ‘locks’ and mean the whole set of synchronization mechanisms, hence my confusion over lock-free threading.

Having said that I will now forgo rigor and refer to all synchronization mechanisms as ‘locks’ because it’s pithier.

Use the smallest number of locks possible.

Don’t treat locks like magic pixie dust and sprinkle them everywhere. Synchronization locks provide safety but at a cost in performance and a greater potential for bugs. Yes, it’s kind of paradoxical.

Hold locks for the shortest length of time possible.

A critical section, for an example, allows only one thread to enter a section of code at a time. The longer the execution time of the section, the longer the window for other threads to box car up waiting for entry.

If there are values that can be computed before entering a critical section, do so. Only the statements that absolutely must be protected should be within the section.

Sometimes a value needs to be retrieved from a synchronized source, used as part of a computation, and a product of the computation needs to be stored to a synchronized destination. If the whole operation does not need to be atomic then, despite the desire to minimize locks, two independent synchronization locks could be better than one. Why? Because the source and destination are decoupled and the combined lock hold time is reduced because only the source read and the destination write are covered.

Be DRY - Don’t Repeat Yourself.

Always a good policy, being DRY has special importance with threaded code. There should be only one expression or implementation of any given synchronized operation. Every thread should be executing the same code to ensure that the operation is performed consistently.

Design to ensure that every acquisition of a lock is balanced by a release of the lock.

Take advantage of the RAII pattern or the dispose pattern or whatever is appropriate to the language and platform. Don’t rely on the developer (even when the developer is yourself) to remember to explicitly add every release for every acquisition.

Finish the threads you start.

Don’t fire and forget. Wait or join and clean up your threads and their resources.

Don’t let threads you created outlive the primary thread in the process. Some platforms have the unfortunate design of performing runtime set up and initialization, calling the program code, and then tearing down and de-initializing on the primary thread. Other threads that may still be running after the primary thread exits may fail when the runtime is pulled out from underneath them.

Don’t kill a thread. That will leave data in an undeterminable state. If appropriate implement a way to signal your thread to finish.

Don’t get focused on locking the data when it’s the operation that needs to be synchronized.

It’s easy to get fixated on the shared data but often times the design is better served by paying more attention to the shared operations.

Avoid nested locks.

Acquiring lock A and then lock B in one part of the code and elsewhere acquiring lock B and then lock A is asking for a deadlock. No-one sets out to write a deadlock. But sometimes the code isn’t exactly what was intended or a change is made and the impact of the change isn’t fully understood. If locks are never nested then the potential for this kind of deadlock just doesn’t exist.

If locks must be nested then alway acquire the locks in the same order and always release the locks in the same opposite order. And be DRY about it.

Avoid method calls within a lock.

This may seem aggressively limiting but method implementations can change and introduce new issues like unwanted nested locks. Keeping method calls out of the scope of locks reduces coupling and the potential for deleterious side effects.

(I think of this one as following in the spirit of LoD.)

Encapsulate intermediate values.

Avoid creating inconsistent state in shared data. Operations that create intermediate values shouldn’t be performed in situ on shared objects or data structures. Use local temporaries for intermediate values. Only update the shared data with the end products of a operation.

Be careful of using the double check lock pattern.

It’s a wonderful idea that’s so clever that’s it’s broken in many languages and runtime platforms.

Where the DCLP is not just inherently broken and can be used, it still needs to be implemented correctly. Mark the variable under test as ‘volatile’ (or the closest equivalent) so that the language compiler doesn’t optimize away one of the two checks. Don’t operate directly on the variable under test. Use a temporary to hold intermediate values and wait to assign to the variable under test until the new state is fully computed.

The DCLP is often used for one time or rare initializations.

Don’t lock the ‘this’ pointer.

If there are member functions of an object that need to synchronize, create a private member variable that is used as the lock target rather than locking on ‘this’. In addition to encapsulating the locking, using a private member guards against potential issues where a framework, library, or runtime may be performing synchronization by using the ‘this’ pointer.

Explain your code.

Leave comments for yourself and the next person.

Strive to make the design grokkable.

The original design and implementation can be bug free but if the design is hard to understand and follow, bugs can be introduced in maintenance cycles. Even if you are the only developer who may ever touch the code, remember that the details of the design that are so crystal clear in your mind today will get supplanted by tomorrow’s events and miscellany like … Squirrel!

.Net Stopwatch Returns Negative Elapsed Time

The System.Diagnostics.Stopwatch class in the .Net runtime can be used to time operations. Bit there’s a problem in .Net 3.5 and earlier. The Elapsed* properties (Elapsed, ElapsedMilliseconds, ElapsedTicks) will sometimes return a negative value.

There’s a note in the documentation:

On a multiprocessor computer, it does not matter which processor the thread runs on. However, because of bugs in the BIOS or the Hardware Abstraction Layer (HAL), you can get different timing results on different processors. To specify processor affinity for a thread, use the ProcessThread.ProcessorAffinity method.

Some advocate setting processor affinity to eliminate the possibility of a negative elapsed time. But a common work-around is to simply use zero whenever the elapsed time is negative.

In recent work I used the Stopwatch class to time operations performed by Threads in a ThreadPool and I was getting false timings because of the negative elapsed time issue. I was leery of messing with the threads’ processor affinities because the ThreadPool is a shared resource. But I had concerns about the ‘convert to zero’ work-around.

Through a Microsoft contact I was able to confirm that the work-around of checking for ‘elapsed < 0’ is adequate. The negative time issue occurs only when measuring very small time periods. The issue is fixed in .Net 4.0 and the fix in the Stopwatch is to check that if elapsed < 0 then set elapsed to zero.

The Only Way To Improve Software Is To Change It

One place I worked had a major release of their enterprise software and following the release a bug appeared. A serious bug. The business was not happy. The Director of Software Engineering was under pressure. The bug had never been reported before and the system processed a large volume of activity every day and had done so since the last major release six months ago. The Director was confident the bug was introduced with the new release. Because of the sense of urgency he didn’t wait for a full analysis. He made the decision to rollback to the prior version. But he was wrong. Rolling back didn’t fix the issue. The bug was pre-existing. It was unfortunate timing that the first time conditions were right to trigger the bug was the day after a major release.

People sometime refer to software as ‘hardening’ in Production. There’s a pervasive presumption that working Production software is generally safer and less risky than newly developed software. The idea is that the longer the software has been in use in Production without change the less likely it is to have unknown bugs. But hardening is a misnomer. Software doesn’t cure like concrete.

The assumption behind ‘hardening’ is that Production is the ultimate test. That the volume and variety of a Production environment is more comprehensive than any test regimen could be. But how good of a test is Production? What if 80% of typical Production activity exercises only 20% of the code? What happens when the unusual occurs? Many organizations test for load and scale issues but I don’t hear of organizations commonly measuring (or inferring) code coverage in Production. Could the unit tests and QA tests actually be more comprehensive than typical Production scenarios? Could ‘hardening’ represent an unsafe assumption?

A colleague pointed out there can also be an anthropomorphizing factor at work. Who do you trust more — the new recruit or the veteran? Even with known flaws the veteran can seem like a safer choice than the unknown recruit. “The devil you know is better than the devil you don’t.” But software releases are not people and you may not know the devil you know as well as you think you know it.

Program testing can be used to show the presence of bugs, but never to show their absence.

Edsger W. Dijkstra

I once wrote the Dijkstra quote on a whiteboard as part of a presentation. The technical folk in the room nodded in recognition and agreement. The non-technical manager became agitated and vehemently disagreed.

To manage risk an organization working with the supposition that ‘hardened’ Production software is safer, may place strict controls on Production releases. Because of the effort involved in a release, releasing quarterly may be considered a fast schedule. Because of the amount of time between releases, each release tends to be large which increases the perceived risk.

If the potential for regression issues can be minimized (say through automated unit tests), which becomes the greater risk — introducing new bugs with a new release or not fixing latent bugs in the existing Production code? If the risk is in not releasing, than releases need to happen faster. Instead of quarterly what if releases were biweekly? The work would be chunked into smaller more frequent releases. Bugs which aren’t critical enough to require a special immediate fix could be fixed in production in 2 weeks instead of 3 months. The software could potentially adapt more quickly to changing needs and user feedback.

Build unit tests. Release often. The only way to improve software is to change it.

Hacking SLN Files to Improve Visual Studio/Visual Source Safe Integration

There are some who think Microsoft Visual Source Safe (VSS) is great. There are some who think that it’s not great but it is pretty good. And there are some who think Source Safe is just good enough. I am not among any of those groups.

One pain point with Source Safe in a Microsoft tool chain is integration with Visual Studio. Historically Visual Studio has stored source control information directly in the solution and project files. This reveals itself to be a really terrible idea the first time a project or solution file is shared in Source Safe. Checkouts from within the IDE will use the the source control bindings in the solution or project. If the solution or project file has been shared and not changed the bindings will be pointing to the original location in Source Safe. Checking out through Visual Studio will checkout the wrong file.

Yes, the solution and project files can be branched and the bindings updated. That’s sub-optimal. It means branching just to fix a tool problem and not because of any actual divergence in the code base. Any non-divergent changes to the solution or project must then be manually propagated to all the branched versions. Ugh.

The problem has not gone unnoticed at Microsoft. Over time and new releases of VStudio and VSS, improvements have been made.

My current position of employment is not using the latest and greatest. We’re on Visual Studio 2005 and Visual Source Safe 2005. The code base is in C#. I worked out a way to have shared solution and project files for non-web projects. Web projects in 2005 are different beasts and don’t follow the same rules. If you are using different versions of Visual Studio or Visual Source Safe your mileage will vary.

When getting files from VSS into a working directory, Visual Source Safe 2005 will create or update a mssccprj.scc file if any of the files present include a solution (.sln) or project file (for C#, .csproj). The mssccprj.scc file is a text file and it will contain the VSS bindings for the solution and project files that are in the same directory as the mssccprj.scc file.

In Visual Studio 2005, project files by default use the special ‘SAK’ string in the project file settings for VSS bindings. SAK indicates to Visual Studio that the bindings in the mssccprj.scc file should be used. The mssccprj.scc bindings are based on the directory retrieved from Source Safe. This means that shared project files just work. Yay.

In 2005 the problem is with solution files.

More specifically the problem is with solution files that include project files that are not in the same directory as the solution file. Creating a MyHelloWorld project will create a MyHelloWorld.sln and a MyHelloWorld.csproj in a MyHelloWorld directory. The .sln will reference the .csproj by name only and both files will have bindings in the mssccprj.scc file in the same directory and that all works without issue. But create a blank solution and add multiple existing projects to it and Visual Studio 2005 reverts to storing specific VSS bindings for the projects in the solution file.

There’s a work-around but it doesn’t work for parenting paths. Any time a solution references a project where the path to the project file starts with ‘..’, Visual Studio will revert to storing a VSS binding in the solution file. Because of this limitation it becomes convenient to have a convention that solution files generally go in the root of the code base tree.

The goal is to get the VSS bindings out of the solution file and have Visual Studio rely on the mssccprj.scc file. I haven’t found a reliable way to do this from within the IDE with existing projects but the solution (.sln) files are just text files, so I hack them.

Here’s a snippet from a .sln file. In Source Safe, the solution file is in “$/CodeBase v1”. There are two projects: a class library and a windows application. The SccProjectName<N> values are bindings to specific locations in the source safe repository. These are the bindings that need to be removed.


Global
GlobalSection(SourceCodeControl) = preSolution
SccNumberOfProjects = 3
SccLocalPath0 = .
SccProjectUniqueName1 = ClassLibrary1\\ClassLibrary1.csproj
SccProjectName1 = \u0022$/CodeBase\u0020v1/ClassLibrary1\u0022,\u0020TAAAAAAA
SccLocalPath1 = ClassLibrary1
SccProjectUniqueName2 = WindowsApplication1\\WindowsApplication1.csproj
SccProjectName2 = \u0022$/CodeBase\u0020v1/WindowsApplication1\u0022,\u0020WAAAAAAA
SccLocalPath2 = WindowsApplication1
EndGlobalSection

The .sln file can be edited to remove the SccProjectName<N> values but the SccLocalPath<N> must be updated to ‘.’ and a new property, SccProjectFilePathRelativizedFromConnection<N>, must be added with the old local path value and an appended directory separator.


Global
GlobalSection(SourceCodeControl) = preSolution
SccNumberOfProjects = 3
SccLocalPath0 = .
SccProjectUniqueName1 = ClassLibrary1\\ClassLibrary1.csproj
SccLocalPath1 = .
SccProjectFilePathRelativizedFromConnection1 = ClassLibrary1\\
SccProjectUniqueName2 = WindowsApplication1\\WindowsApplication1.csproj
SccLocalPath2 = .
SccProjectFilePathRelativizedFromConnection2 = WindowsApplication1\\
EndGlobalSection

Reference: Alin Constantin’s blog: The SAK source control strings in Visual Studio’s project files

Professional Praise

Here’s a story:

I’m working as an in-house software engineer for Nameless Big Co creating software for internal use.

I’m at an all-hands meeting for my business unit group. A very important person in a nice expensive suit is at the podium. Apparently we’re honored to have him come and speak to us. What he has to say is engaging until he gets to a certain point.

He tells us he’s had a career in financial services IT and we’re the best and brightest IT organization he has ever worked with. I think of the inefficiencies and poor decisions we deal with every day. It’s normal stuff for a large organization and for a software development management chain heavy on MBA’s. I don’t think we’re more clever than average.

Why is Mr. VIP laying on the superlatives? Is he out of touch? Is he measuring differently? Is he just trying to be a cheerleader? Is he marketing to us?

Striving to be the best and the brightest is incompatible with being uncritical enough to accept his hyperbole. I tune out. He’s pushing more noise than signal.

Here’s a second story:

The lead architect has moved some of my code from a particular project down into a core library so he could use it on another project. “You saved me a lot of time.” He tells me. “You did some good work on that project and I want to leverage it across the other projects.”

Here’s a guy whose technical chops I respect and he found my code useful. It’s a small thing but it made my day.

Peer praise is meaningful.

Internet Explorer's Maximum URL Length

Note to self: Internet Explorer has a maximum URL size limit of 2,083 characters. Passing a longer URL (like maybe a URL with a dynamically built query string) to IE 7.0 will produce a cryptic error page (even with ‘Friendly’ turned off) that says “Internet Explorer cannot display the webpage” and then misleadingly suggests that the server can not be reached. Don’t waste time troubleshooting network connections or DNS lookups. Don’t be puzzled by why the same URL works in FireFox and Safari.

Theory P: Software Development is Probabilistic

In my head I’ve been struggling to write a blog post about how thinking of software development as a manufacturing process is wrong. The catalyst for getting some semblance of my thoughts down was reading Reg Braithwaite’s Which theory fits the evidence?

Have you seen a car ad that shows a factory floor and an efficient automated assembly line? That’s not like software development. Have you seen a car ad that shows a design studio with a clay mock-up? That’s like software development. The guy on the factory floor can easily know how long it takes to assemble an automobile. It’s much more difficult for the guy sculpting clay to predict how much work and how many revisions it will take to get to the final design.

Software development is an exploration. It’s learning about the problem domain, the customer’s needs, and how a solution can be realized with the available resources. Learning requires space and time for experimentation and failure.

Software manufacturing, at its least adorned, is a file copy operation.

It’s tempting to believe that with enough upfront planning, analysis, and specification any software project can be made predictable and problem-free. It’s a false notion. Time is always limited. Humans are fallible. Something will always be missed or misunderstood or not understood deeply enough or new information will come to light or business conditions will change. A development process must accept and adapt for unforeseen change or it’s broken.

Avoid Order Dependencies between Object Methods

My code was calling a certain method on a certain object. I was passing all the right parameters and the method was returning garbage. What the hell.

It turned out that I needed to call another method of the same object first.

An object that depends on its methods being called in a specific order is a bad idea. There's no language that I'm aware of that supports defining ordering dependencies for the methods of an object. If I make calls in the wrong order the compiler or runtime can't tell me I've made an error. I can't treat such an object as a black box. It's leaky. I'm forced to learn something about its internals. It defeats part of the point of encapsulation. Objects should be able to handle method calls in any order.

For the code I was writing I couldn't change the offending object. It's technical debt but it wasn't my call. The best I could do was add comments so the issue wouldn't be a hidden surprise for the next developer.

A minimal fix would have been to enforce the ordering constraint by having the method call fail if the prerequisite method hadn't been called. This is crude but effective.

Since this tactic implies keeping state in the object to track if the prerequisite method has been called, it might be nice to simply call the prerequisite if it hasn't been already. If the object has a method that is a prerequisite to a large number of other methods there would be this little bit of check code duplicated across all the dependent methods. That's a little ugly. And it's not DRY. (It's one of the reasons I don't like two stage construction.) So using object state as the principal means to solve the issue may not be the best solution.

A different approach might leverage the object design. Maybe these methods shouldn't all be on the same object. The 'prerequisite' method could be a factory method that returns an object that contains the other methods. If appropriate the factory could cache the last computed object.

In a well behaved object order dependencies among methods should be either designed out or encapsulated as an implementation detail.

Design Patterns are Good

I’m suspicious of a software developer who rejects design patterns.

I’m sympathetic and sad if someone’s only experience with design patterns has been ugly. Yes, I have experienced code bases that were nasty over-engineered messes and that were claimed to be design pattern based. I don’t take that as reason to reject design patterns. The best and brightest ideas can be poorly implemented.

Design patterns shouldn’t be treated as ready-to-wear solutions. The Gang of Four book is not a cook book. And you don’t get points for using every possible pattern in one project.

The greatest value of patterns may be as abstract constructs for thinking and communicating about a software design. Patterns are a shorthand for expressing complex designs.

Link: How to Use Design Patterns.

There seems to be an anti-pattern meme that ranges from “I don’t want to try that” (like my six year old who won’t eat vegetables) to a virulent “I don’t need ivory tower astro-architecture.” An anti-intellectual attitude towards design patterns is a problem because software design is an intellectual activity. Further good software design can’t be simply handed down from the architect, it is the responsibility of the whole development team.

Sometimes people get set in their ways. “I’ve been doing this in this way for as long as I’ve been doing this. Why should I change now?” Well, because one of the constants in software development is constant change. It’s a young discipline. A good software developer is always developing and honing his or her skills. (Actually to be good at anything requires constantly developing and advancing your skills.)

When a software developer rejects design patterns, which are really just about communicating common ideas, it gives me pause. Is this person misinformed or scarred by a bad experience? Or has this person given up on learning new ideas?

Open a Command Shell from the Windows Explorer

It’s hugely convenient to be able to pop open a command shell (aka “Command Prompt”) in a given directory from the Windows Explorer. The Registry can be manually edited to add context menu commands and there is a Microsoft Knowledge Base article that describes one such approach but the PowerToys collection includes an “Open Command Window Here” item that’s a little more complete. The PowerToy adds shell commands for directories and drives.

Link: How to start a command prompt in a folder in Windows Server 2003, Windows XP, and Windows 2000.

Link: Microsoft PowerToys for Windows XP.

Apparently Vista includes an “Open Command Window Here” command but only on a Shift-Right-Click in the Explorer’s right hand pane.

Incidentally I always need to change the factory defaults for the Command Prompt. I have no great sentiment for monochrome displays and can’t see any sense to using white characters on a black background. I also change the font from ‘Raster’ to ‘Lucida Console’, enable ‘Quick Edit Mode’, increase the command history, increase the height of the screen buffer, and increase the height of the window.

Encapsulating a Thread in a C++ Object

It’s a frequently asked question in C++ forums and newsgroups. Try to create a thread and pass an instance member function of a C++ class as the thread start routine and the compiler balks. What is the compiler complaining about? How can a thread be run within an instance of a class?

The Microsoft C runtime library (MSCRT) includes the _beginthreadex1 routine.

uintptr_t _beginthreadex(
    void *security,
    unsigned stack_size,
    unsigned ( __stdcall *start_address )( void * ),
    void *arglist,
    unsigned initflag,
    unsigned *thrdaddr
);

_beginthreadex expects a pointer to a function, a function that will be executed by the newly created thread. It’s the start_address parameter in the function definition above.

If a non-static member function of a class is passed to the _beginthreadex function as the start_address parameter, the code will fail to compile. (With the Microsoft compiler the error might be one of the following depending on the specific syntax used: C3867, C2276, or C2664.)

Members of a C++ class can be static or non-static. Static members are sometimes known as class members. Non-static members are sometimes known as instance members. Instance member functions operate on a specific instance of a class. They are able to do so because the C++ compiler provides instance member functions with a ‘this’ pointer as a hidden function argument. The hidden extra argument is the source of the compiler’s consternation. An instance member function can’t be passed as the start_address parameter.

Static member function don’t have the special ‘this’ pointer magic. Can a static member function be passed as the start_address? Yes. But if the goal is to create a class whose instances are separate threads, passing a static member function would seem at first to not be much of an advance towards a solution.

As a student I was told that static member functions can not access non-static data members or call non-static member functions. It’s a simplistic statement that implies a prohibition that doesn’t exist. Statics have no knowledge of any specific instances of a class. There’s no ‘this’ pointer in a static member function. But static functions have the same permissions on the class as non-static functions. In other words if a static function is given a ‘this’ pointer it can use the ‘this’ pointer to access the data and functions of a specific instance.

All that needs to be done is to arrange to pass a ‘this’ pointer to the static member function. _beginthreadex has an arglist parameter for passing arguments to the thread start routine. Below is example code that implements this technique.

A couple of things to note:

  • Data that the thread needs to operate on can be stored in the object instance. Only the ‘this’ pointer needs to be passed via the thread start routine.
  • The general technique of using a static member function to forward to a specific object instance can be applied to encapsulating any C language style callback.

Windows thread class

This code was written and tested with Visual Studio 2005.

#define WIN32_LEAN_AND_MEAN
#include <process.h>
#include <windows.h>
#include <iostream>

class thread
{
public:
    thread();
    virtual ~thread();

    const HANDLE& GetHandle() const { return m_hThread; }
    const bool wait() const;

private:
    // copy operations are private to prevent copying
    thread(const thread&);
    thread& operator=(const thread&);

    static unsigned __stdcall threadProc(void*);
    unsigned run();

    HANDLE m_hThread;
    unsigned m_tid;
};

thread::thread()
{
    m_hThread = reinterpret_cast<HANDLE>(
        ::_beginthreadex(
                0, // security
                0, // stack size
                threadProc, // thread routine
                static_cast<void*>(this), // thread arg
                0, // initial state flag
                &m_tid // thread ID
            )
        );
    if (m_hThread == 0)
    {
        throw std::exception("failed to create thread");
    }
}

thread::~thread()
{
    try    
    {
        ::CloseHandle(GetHandle());
    }
    catch(...)
    {
        // suppress any exception; dtors should never throw
    }
}

const bool thread::wait() const
{
    bool bWaitSuccess = false;
    // a thread waiting on itself will cause a deadlock
    if (::GetCurrentThreadId() != m_tid)
    {
        DWORD nResult = ::WaitForSingleObject(GetHandle(), INFINITE);
        // nResult will be WAIT_OBJECT_0 if the thread has terminated;
        // other possible results: WAIT_FAILED, WAIT_TIMEOUT,
        // or WAIT_ABANDONED
        bWaitSuccess = (nResult == WAIT_OBJECT_0);
    }
    return bWaitSuccess;
}

unsigned __stdcall thread::threadProc(void* a_param)
{
    thread* pthread = static_cast<thread*>(a_param);
    return pthread->run();
}

unsigned thread::run()
{
    std::cout << "Hello from thread" << std::endl;
    return 0;
}

int main(int argc, char* const argv[])
{
    try
    {
        std::cout << "Hello" << std::endl;

        thread thrd;
        thrd.wait();

        std::cout << "Done" << std::endl;
    }
    catch (std::exception& ex)
    {
        std::cerr << ex.what() << std::endl;
    }
    catch (...)
    {
        std::cerr << "unknown exception" << std::endl;
    }

    return 0;
}

pthreads thread class

Just so you don’t think I’m Windows-centric, below is a pthreads version of the example code. This example was written and tested in Xcode on Mac OS X.

#include <iostream>
#include <exception>
#include <pthread.h>

class thread
{
public:
    thread();
    virtual ~thread() { }

    const pthread_t& getTID() const { return m_tid; }
    bool join();

private:
    // copy operations are private to prevent copying
    thread(const thread&);                // no implementation
    thread& operator=(const thread&);    // no implementation

    static void* threadproc(void*);
    void* run();

    pthread_t m_tid;
};

thread::thread()
{
    int nerr = ::pthread_create(
            &m_tid,
            0,
            threadproc,
            static_cast<void*>(this)
        );
    if (nerr)
    {
        // for brevity a string instead of a custom exception
        throw std::string("failed to create thread");
    }
}

bool thread::join()
{
    // pthread_join will detect attempts to join a thread to itself
    int nerr = ::pthread_join(getTID(), 0);
    return (nerr == 0);
}

void* thread::threadproc(void* a_param)
{
    thread* pthread = static_cast<thread*>(a_param);
    return pthread->run();
}

void* thread::run()
{
    std::cout << "Hello from thread" << std::endl;
    return 0;
}

int main (int argc, char* const argv[])
{
    try
    {
        std::cout << "Hello" << std::endl;

        thread thrd;
        thrd.join();
        
        std::cout << "Done" << std::endl;
    }
    catch (std::string& ex)
    {
        std::cerr << ex << std::endl;
    }
    catch (std::exception& ex)
    {
        std::cerr << ex.what() << std::endl;
    }
    catch (...)
    {
        std::cerr << "unknown exception" << std::endl;
    }

    return 0;
}

Related Posts:
Disallowing Copying in a C++ Class

1_beginthreadex is a wrapper that performs some library specific housekeeping and calls the Win32 API’s CreateThread function. If the MS C runtime library is being used, it’s best to use the wrapper and not call CreateThread directly.

Using Properties and Tuples to Get Multiple Values from a JavaScript Function

Here’s a word: expando. Here’s a sentence using the word: JavaScript has expando objects.

Expando sounds like a silly made-up word. And it might be. But it’s also an actual word in Latin. It means ‘to spread out.’ The Latin meaning is almost appropriate for JavaScript. Objects in JavaScript are expando objects because properties can be dynamically added (expanding the object) and removed.

var obj = new Object();

// add a property
obj.foo = 'bar';

// remove a property
delete obj.foo;

Since functions in JavaScript are first class objects, properties can be added to functions. A function can be made state-ful by setting a property. In the example below a function named process sets an error property on itself:

function process(input)
{
    process.error = 0;
    ...
    if (...)
    {
        process.error = 1;
    }

    return output;
}

If the process function has never been run, process.error will be undefined.

if (typeof(process.error) != 'undefined')
{
    // test for an error
    ...

In newer versions of JavaScript this test could be written more directly as (process.error != undefined) but using the typeof operator is more portable.

The undefined state could be eliminated by initializing the property outside the function:

var process = function(input) { ... };
process.error = 0;

Are state-ful functions a good practice? It would be problematic if the function can be called again before the previous state has been handled so, in general, keeping state in a function should probably be avoided.

Yes, I just said don’t do the thing I just showed how to do.

Instead of creating state within the function, the function can return a tuple. (Like expando, tuple has a Latin etymology. Did you think you would be learning so much Latin from a blog post on JavaScript?) Tuple is a term used in set theory and by extension the relational database model but in terms of functional programming a tuple is an object that holds several other objects.

A tuple could be implemented as an Array. Using an array would require knowing the position in the array of each specific datum. Depending on positional information is brittle especially for large sets. A better way is an implementation that supports named datums. With named datums the order is irrelevant and the code is more literate.

Here’s the process function modified to use an ad-hoc tuple:

function process(input)
{
    var tuple = new Object();
    tuple.output = null;
    tuple.error = 0;
    ...
    if (...)
    {
        tuple.error = 1;
    }

    return tuple;
}

Using the object literal syntax the tuple object and its properties can be created in one line instead of three:

    var tuple = {output: null, error: 0};

Finally the returned tuple result could be handled as follows:

var result = process(in);
if (result.error == 0)
{
    // do something with result.output
    ...
}

Related Posts:
JavaScript Closures