Click here to Skip to main content
Click here to Skip to main content

Cross Platform Threads

, 15 Oct 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
A cross platform thread class.

Introduction

When writing cross platform applications targeting Windows and any Unix flavored OS, working with different types of threads can be complex. There are two types of threads: detached and joinable. POSIX thread APIs support both Joinable and Detached threads while Win32 multithreading APIs support only Joinable threads.

POSIX threads are available on all Unix based platforms (MAC, Linux …), but are not natively supported on Microsoft Windows. There are some open source alternatives which emulate POSIX threads for the Win32 environment, such as pthreads-win32.

The fundamental difference between Joinable and Detached threads is that Joinable threads can be waited for, while Detached threads cannot be waited for. It is required to wait for a Joinable thread, or else system resources used by it will never be freed. In contrast, you only need to start a Detachable thread and it will terminate on its own.

The XPThreads class provides a simple interface for spawning new threads, and at the same time hides complexities involved in using different threading packages. Internally, XPThreads uses Joinable threads for Windows (only possible threads) and Detachable threads for Unix based OSs (POSIX threads). The code can easily be tweaked, if required, for using Joinable threads on Unix based OSs.

Background

This is my first article on CodeProject. I came across a requirement for providing multithreading support while working on an X-Platform plug-in. One alternative was to use some heavy external thread package which I wasn’t inclined for; hence, I decided to write my own X-Platform thread class.

I have tested this class on Macintosh and Windows, but I strongly believe that this should work alright on any Unix based OS.

Using the code

Using the XPThreads class is simple.

You need to add XPThreads.h and XPThreads.cpp in your project workspace and include the XPThreads.h file in those source files which require to spawn new threads.

The next step is to make a thread callback function. This function should be a global function or a static class member function. It should strictly follow the following function prototype:

unsigned long Entry (void* param)
{
    cout<<"In the thread" << endl;
    return NULL;
}

The second step is to create a new object of XPThreads and supply it with the function address for the thread entry procedure. This can be done in two ways, as evident below:

In the constructor:

XPThreads* ptrThread = new XPThreads(Entry);
ptrThread->Run();
delete ptrThread;

Or:

XPThreads* ptrThread = new XPThreads();
ptrThread-> SetThreadProc(Entry);
ptrThread->Run();
delete ptrThread;

The final step is to call Run() using the newly created XPThreads object.

Downloads

Demo project

The demo project includes the project files for Windows and Macintosh platforms along with the source code. It demonstrates how XPThreads can be used on Macintosh and Windows.

Source files

Source files include “XPThreads.h” and “XPThreads.cpp” which can be used separately in your application.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Amit Gupta MDS
Product Manager MetaDesign Solutions
India India
Senior Software Developer with special skills in Developing Applications for Macintosh and Windows.

Comments and Discussions

 
GeneralMy vote of 1 PinmemberLen Holgate21-Apr-10 5:15 
Questionflawed assumptions? PinmemberLen Holgate21-Apr-10 5:15 
QuestionHow do you send arguments with the thread function? PinmemberOskar12318-Nov-08 5:40 
GeneralDownload links are not working PinmemberSceptic Mole15-Oct-06 23:50 
QuestionWhere is the article? PinmemberSceptic Mole15-Oct-06 23:48 
AnswerRe: Where is the article? PinmemberAmit Gupta XP16-Oct-06 0:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 16 Oct 2006
Article Copyright 2006 by Amit Gupta MDS
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid