TweetFollow Us on Twitter

Jan 01 Online Volume Number: 17 (2001)
Issue Number: 1
Column Tag: MacTech Online

MacTech Online

by Jeff Clites <online@mactech.com>

In our previous column we began coverage of a set of technologies that I'm calling the "New Basics," in recognition that "the things that every programmer should know" are changing, and as they change we're converging on a set which is the same for all programmers-there are no longer different sets for different platforms. This is in part because the fundamental, low-level differences between operating systems are shrinking, and in part because the focus is shifting toward technologies which are new to all platforms, and which are being embraced by all platforms as they emerge. We also noted that this is leading to a sort of impromptu cooperation, and companies like IBM which used to be archetypal bad guy are now great sources of information for Macintosh programmers who are new to the Unix side of computing. Our first New Basic was XML, which is the most-hyped technology of the day, and is likely to dominate information interchange for some time to come. This month we are going to take a look at threading-both how it will be conceptually different under Mac OS X than under Mac OS 9, and how it will be more important there as well.

Preemptive Threading

Most Macintosh developers will be accustomed to a "cooperative" environment, where an application has exclusive use of the processor (and other system resources) until it explicitly yields control to other waiting processes. Threading under Mac OS 9 is also typically cooperatively scheduled, so that threads within a single application have to explicitly yield for other threads to proceed. This type of scheduling simplifies certain issues surrounding concurrent access to memory and system resources, but it can be difficult for a developer to know how often, and when, to yield the processor in order to provide the best overall system performance. Also, the majority of the Toolbox is not thread safe, so that you have to be very careful about the types of operations you perform outside of the main thread. As a result, under Mac OS 9 applications tend to be single-threaded if at all possible.

Under Mac OS X, as under other Unix-based operating systems, both processes and threads are scheduled preemptively, so that the kernel makes all scheduling decisions and a thread or process can be interrupted at any time. From the point of view of a programmer, this means that you can never be sure that two operations within your application will take place without another process interrupting. The benefit of this approach is that you never has to explicitly code time-consuming sections of your application to yield the processor in order to prevent freezing the user's system for long periods, and you can perform blocking I/O operations in a thread and know that other threads and processes will happily proceed while yours is waiting for data to be read transferred. This simplifies many uses of threads, but introduces complications of its own: you have to guard critical sections of code and data from concurrent access if they are going to be used by multiple threads (even reading data from a variable cannot be assumed to be an atomic operation), and you can't assume that the state of the system has not changed from one line of code to the next (for instance, it isn't reliable to check for the existence of a file and then try to open it as a separate step, because the file could be deleted in between the two operations, even if they occur back-to-back within your program). Preemptive scheduling requires a certain paranoia on the part of the programmer, but it leads to an overall more responsive and reliable system, and it allows simple applications to be written simply, without worrying that they might accidentally hijack the processor. And of course, threading makes it possible for a single application to take advantage of multiple processors on systems which have them, and in fact on such systems two threads within a single application can be executing at literally the same time.

POSIX Threads

The fundamental threading library under Mac OS X is POSIX Threads, or Pthreads for short. This will be new to veteran OpenStep programmers as well as to classic Macintosh programmers, as Mac OS X Server and previous NeXT-derived operating systems used a different threading library, called Cthreads. The big benefit of Pthreads is that applications using them should be portable to other POSIX-compliant operating systems (if the rest of the code is portable, of course), and this will make it that much easier to bring standard Unix tools and applications over to the Macintosh. For most applications it won't be necessary to use the Pthreads library directly, as Cocoa has its own threading facilities which provide a higher-level view and insulate the programmer from the details of the underlying threading library. Even so, the conceptual model remains the same, and Macintosh programmers which are new to preemptive threading will benefit from the wealth of information there is available on POSIX threading. Also, if you plan to develop (or port) more traditional Unix-style applications you will need to know about the lower-level libraries in more detail.

A good starting point is the article How to program with threads on SunWorld Online, which talks about programming with threads in general, and the advantages that they offer. Next, IBM's developerWorks site has a series of articles which cover POSIX threads specifically. Like many of their articles, these are written with a Linux audience in mind, but most of the information is nevertheless relevant, as POSIX is explicitly cross platform. These articles will give you a good feel for the issues involved, and point out the potential complexities of using threads.

After you've gotten a handle on the major concepts, you'll want to find more in-depth coverage of the details of using Pthreads. There are several online tutorials which can help. In particular, Getting Started With POSIX Threads is a brief tutorial which will help solidify some key points, and then you can move on to more comprehensive coverage in Multi-Threaded Programming With POSIX Threads and POSIX Threads Programming, both of which are quite thorough.

If you are curious about other issues surrounding threading, you should check the comp.programming.threads FAQ. Also, if you are interested in the different possible strategies for actually implement a threading library, the Navy has an article which covers the strengths and weaknesses of three different approaches. Finally, you may want to visit the Programming POSIX Threads web site, which has pointers to further information.

There are several books about Pthreads, and I always find it helpful to have a detailed printed reference to augment all of the web-based information. Programming with POSIX Threads by David R. Butenhof (ISBN: 0-201-63392-2) is my favorite and is often recommended by others, but there are several alternatives available, including Pthreads Programming (ISBN: 1-56592-115-1) and Multithreaded Programming With Pthreads (ISBN: 0136807291). Take a look and see which one suites you the best.

Now you should be up to speed on basic number two. Keep your eye on this column in coming months, because we're not done with the New Basics yet!

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »
Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links below... | Read more »
Marvel Future Fight celebrates nine year...
Announced alongside an advertising image I can only assume was aimed squarely at myself with the prominent Deadpool and Odin featured on it, Netmarble has revealed their celebrations for the 9th anniversary of Marvel Future Fight. The Countdown... | Read more »
HoYoFair 2024 prepares to showcase over...
To say Genshin Impact took the world by storm when it was released would be an understatement. However, I think the most surprising part of the launch was just how much further it went than gaming. There have been concerts, art shows, massive... | Read more »
Explore some of BBCs' most iconic s...
Despite your personal opinion on the BBC at a managerial level, it is undeniable that it has overseen some fantastic British shows in the past, and now thanks to a partnership with Roblox, players will be able to interact with some of these... | Read more »
Play Together teams up with Sanrio to br...
I was quite surprised to learn that the massive social network game Play Together had never collaborated with the globally popular Sanrio IP, it seems like the perfect team. Well, this glaring omission has now been rectified, as that instantly... | Read more »
Dark and Darker Mobile gets a new teaser...
Bluehole Studio and KRAFTON have released a new teaser trailer for their upcoming loot extravaganza Dark and Darker Mobile. Alongside this look into the underside of treasure hunting, we have received a few pieces of information about gameplay... | Read more »

Price Scanner via MacPrices.net

14-inch M3 MacBook Pro with 16GB of RAM avail...
Apple has the 14″ M3 MacBook Pro with 16GB of RAM and 1TB of storage, Certified Refurbished, available for $300 off MSRP. Each MacBook Pro features a new outer case, shipping is free, and an Apple 1-... Read more
Apple M2 Mac minis on sale for up to $150 off...
Amazon has Apple’s M2-powered Mac minis in stock and on sale for $100-$150 off MSRP, each including free delivery: – Mac mini M2/256GB SSD: $499, save $100 – Mac mini M2/512GB SSD: $699, save $100 –... Read more
Amazon is offering a $200 discount on 14-inch...
Amazon has 14-inch M3 MacBook Pros in stock and on sale for $200 off MSRP. Shipping is free. Note that Amazon’s stock tends to come and go: – 14″ M3 MacBook Pro (8GB RAM/512GB SSD): $1399.99, $200... Read more
Sunday Sale: 13-inch M3 MacBook Air for $999,...
Several Apple retailers have the new 13″ MacBook Air with an M3 CPU in stock and on sale today for only $999 in Midnight. These are the lowest prices currently available for new 13″ M3 MacBook Airs... Read more
Multiple Apple retailers are offering 13-inch...
Several Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices... Read more
Roundup of Verizon’s April Apple iPhone Promo...
Verizon is offering a number of iPhone deals for the month of April. Switch, and open a new of service, and you can qualify for a free iPhone 15 or heavy monthly discounts on other models: – 128GB... Read more
B&H has 16-inch MacBook Pros on sale for...
Apple 16″ MacBook Pros with M3 Pro and M3 Max CPUs are in stock and on sale today for $200-$300 off MSRP at B&H Photo. Their prices are among the lowest currently available for these models. B... Read more
Updated Mac Desktop Price Trackers
Our Apple award-winning Mac desktop price trackers are the best place to look for the lowest prices and latest sales on all the latest computers. Scan our price trackers for the latest information on... Read more
9th-generation iPads on sale for $80 off MSRP...
Best Buy has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80 off MSRP on their online store for a limited time. Prices start at only $249. Sale prices for online orders only, in-store prices... Read more
15-inch M3 MacBook Airs on sale for $100 off...
Best Buy has Apple 15″ MacBook Airs with M3 CPUs on sale for $100 off MSRP on their online store. Prices valid for online orders only, in-store prices may vary. Order online and choose free shipping... Read more

Jobs Board

Sublease Associate Optometrist- *Apple* Val...
Sublease Associate Optometrist- Apple Valley, CA- Target Optical Date: Mar 22, 2024 Brand: Target Optical Location: Apple Valley, CA, US, 92307 **Requisition Read more
Early Preschool Teacher - Glenda Drive/ *Appl...
Early Preschool Teacher - Glenda Drive/ Apple ValleyTeacher Share by Email Share on LinkedIn Share on Twitter Read more
Retail Assistant Manager- *Apple* Blossom Ma...
Retail Assistant Manager- APPLE BLOSSOM MALL Brand: Bath & Body Works Location: Winchester, VA, US Location Type: On-site Job ID: 04225 Job Area: Store: Management Read more
Housekeeper, *Apple* Valley Village - Cassi...
Apple Valley Village Health Care Center, a senior care campus, is hiring a Part-Time Housekeeper to join our team! We will train you for this position! In this role, Read more
Sonographer - *Apple* Hill Imaging Center -...
Sonographer - Apple Hill Imaging Center - Evenings Location: York Hospital, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Now See Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.