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

Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
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 »

Price Scanner via MacPrices.net

13-inch M2 MacBook Airs in stock today at App...
Apple has 13″ M2 MacBook Airs available for only $849 today in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty is included,... Read more
New today at Apple: Series 9 Watches availabl...
Apple is now offering Certified Refurbished Apple Watch Series 9 models on their online store for up to $80 off MSRP, starting at $339. Each Watch includes Apple’s standard one-year warranty, a new... Read more
The latest Apple iPhone deals from wireless c...
We’ve updated our iPhone Price Tracker with the latest carrier deals on Apple’s iPhone 15 family of smartphones as well as previous models including the iPhone 14, 13, 12, 11, and SE. Use our price... Read more
Boost Mobile will sell you an iPhone 11 for $...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering an iPhone 11 for $149.99 when purchased with their $40 Unlimited service plan (12GB of premium data). No trade-in is required... Read more
Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, available for $759 for 8-Core CPU/7-Core GPU/256GB models and $929 for 8-Core CPU/8-Core GPU/512GB models. Apple’s one-year warranty is... Read more
Updated Apple MacBook Price Trackers
Our Apple award-winning MacBook Price Trackers are continually updated with the latest information on prices, bundles, and availability for 16″ and 14″ MacBook Pros along with 13″ and 15″ MacBook... Read more
Every model of Apple’s 13-inch M3 MacBook Air...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices are the lowest currently available for new 13″ M3 MacBook Airs among... Read more
Sunday Sale: Apple iPad Magic Keyboards for 1...
Walmart has Apple Magic Keyboards for 12.9″ iPad Pros, in Black, on sale for $150 off MSRP on their online store. Sale price for online orders only, in-store price may vary. Order online and choose... Read more

Jobs Board

Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.