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

Whitethorn Games combines two completely...
If you have ever gone fishing then you know that it is a lesson in patience, sitting around waiting for a bite that may never come. Well, that's because you have been doing it wrong, since as Whitehorn Games now demonstrates in new release Skate... | Read more »
Call of Duty Warzone is a Waiting Simula...
It's always fun when a splashy multiplayer game comes to mobile because they are few and far between, so I was excited to see the notification about Call of Duty: Warzone Mobile (finally) launching last week and wanted to try it out. As someone who... | Read more »
Albion Online introduces some massive ne...
Sandbox Interactive has announced an upcoming update to its flagship MMORPG Albion Online, containing massive updates to its existing guild Vs guild systems. Someone clearly rewatched the Helms Deep battle in Lord of the Rings and spent the next... | Read more »
Chucklefish announces launch date of the...
Chucklefish, the indie London-based team we probably all know from developing Terraria or their stint publishing Stardew Valley, has revealed the mobile release date for roguelike deck-builder Wildfrost. Developed by Gaziter and Deadpan Games, the... | Read more »
Netmarble opens pre-registration for act...
It has been close to three years since Netmarble announced they would be adapting the smash series Solo Leveling into a video game, and at last, they have announced the opening of pre-orders for Solo Leveling: Arise. [Read more] | Read more »
PUBG Mobile celebrates sixth anniversary...
For the past six years, PUBG Mobile has been one of the most popular shooters you can play in the palm of your hand, and Krafton is celebrating this milestone and many years of ups by teaming up with hit music man JVKE to create a special song for... | Read more »
ASTRA: Knights of Veda refuse to pump th...
In perhaps the most recent example of being incredibly eager, ASTRA: Knights of Veda has dropped its second collaboration with South Korean boyband Seventeen, named so as it consists of exactly thirteen members and a video collaboration with Lee... | Read more »
Collect all your cats and caterpillars a...
If you are growing tired of trying to build a town with your phone by using it as a tiny, ineffectual shover then fear no longer, as Independent Arts Software has announced the upcoming release of Construction Simulator 4, from the critically... | Read more »
Backbone complete its lineup of 2nd Gene...
With all the ports of big AAA games that have been coming to mobile, it is becoming more convenient than ever to own a good controller, and to help with this Backbone has announced the completion of their 2nd generation product lineup with their... | Read more »
Zenless Zone Zero opens entries for its...
miHoYo, aka HoYoverse, has become such a big name in mobile gaming that it's hard to believe that arguably their flagship title, Genshin Impact, is only three and a half years old. Now, they continue the road to the next title in their world, with... | Read more »

Price Scanner via MacPrices.net

B&H has Apple’s 13-inch M2 MacBook Airs o...
B&H Photo has 13″ MacBook Airs with M2 CPUs and 256GB of storage in stock and on sale for up to $150 off Apple’s new MSRP, starting at only $849. Free 1-2 day delivery is available to most US... Read more
M2 Mac minis on sale for $100-$200 off MSRP,...
B&H Photo has Apple’s M2-powered Mac minis back in stock and on sale today for $100-$200 off MSRP. Free 1-2 day shipping is available for most US addresses: – Mac mini M2/256GB SSD: $499, save $... Read more
Mac Studios with M2 Max and M2 Ultra CPUs on...
B&H Photo has standard-configuration Mac Studios with Apple’s M2 Max & Ultra CPUs in stock today and on Easter sale for $200 off MSRP. Their prices are the lowest available for these models... Read more
Deal Alert! B&H Photo has Apple’s 14-inch...
B&H Photo has new Gray and Black 14″ M3, M3 Pro, and M3 Max MacBook Pros on sale for $200-$300 off MSRP, starting at only $1399. B&H offers free 1-2 day delivery to most US addresses: – 14″ 8... Read more
Department Of Justice Sets Sights On Apple In...
NEWS – The ball has finally dropped on the big Apple. The ball (metaphorically speaking) — an antitrust lawsuit filed in the U.S. on March 21 by the Department of Justice (DOJ) — came down following... Read more
New 13-inch M3 MacBook Air on sale for $999,...
Amazon has Apple’s new 13″ M3 MacBook Air on sale for $100 off MSRP for the first time, now just $999 shipped. Shipping is free: – 13″ MacBook Air (8GB RAM/256GB SSD/Space Gray): $999 $100 off MSRP... Read more
Amazon has Apple’s 9th-generation WiFi iPads...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Discounted 14-inch M3 MacBook Pros with 16GB...
Apple retailer Expercom has 14″ MacBook Pros with M3 CPUs and 16GB of standard memory discounted by up to $120 off Apple’s MSRP: – 14″ M3 MacBook Pro (16GB RAM/256GB SSD): $1691.06 $108 off MSRP – 14... Read more
Clearance 15-inch M2 MacBook Airs on sale for...
B&H Photo has Apple’s 15″ MacBook Airs with M2 CPUs (8GB RAM/256GB SSD) in stock today and on clearance sale for $999 in all four colors. Free 1-2 delivery is available to most US addresses.... Read more
Clearance 13-inch M1 MacBook Airs drop to onl...
B&H has Apple’s base 13″ M1 MacBook Air (Space Gray, Silver, & Gold) in stock and on clearance sale today for $300 off MSRP, only $699. Free 1-2 day shipping is available to most addresses in... Read more

Jobs Board

Medical Assistant - Surgical Oncology- *Apple...
Medical Assistant - Surgical Oncology- Apple Hill Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply 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
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
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
Business Analyst | *Apple* Pay - Banco Popu...
Business Analyst | Apple PayApply now " Apply now + Apply Now + Start applying with LinkedIn Start + Please wait Date:Mar 19, 2024 Location: San Juan-Cupey, PR Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.