EXPERT ADVICE OS/2 NOTEBOOK • Douglas A. Hamilton

MASTERING OS/2 THREADS

OS/2's unique style of lightweight multitasking taxes developers and rewards users

on't make me wait! When I select Print, insert a new value into a spreadsheet, or resize a D desktop publishing window, I don't want to be ignored while the appli­ cation grinds away. None of us likes to wait, and applications designers have heard that message. So spreadsheets today come with background recalc features, for exam­ ple. But these features have taken forever to arrive, and they still aren't universally provided. If everyone knows what the problem is, why don't they just go ahead and fix it? The answer is that background any­ thing under DOS or Windows has to be custom-made. OS/2 is the first and only normal OS/2 or Unix processes, the sys­ Threads run inside processes, which widely distributed system to provide a tem can create and switch among them in turn run inside screens. The progres­ specific mechanism for dealing with quickly. Because they share memory, sion from threads outward to screens en­ problems of this class without building tasks enjoy high-bandwidth communica­ tails more and more "fire-walling" on everything from scratch. OS/2 lets a de­ tion. the part of OS/2. signer cleave off compute- or I/O-bound The idea isn't completely new. Re­ But the most important distinction is activities as separately scheduled threads searchers in the Unix community, par­ that while processes and screens are nor­ and thereby ensure crisp user interaction ticularly at the Carnegie Mellon Univer­ mally used for sharing the processor be­ at all times. sity Mach project, have talked about tween applications, threads are uniquely lightweight processes for several years. a way of sharing the processor inside an Modes of Concurrency But OS/2 is the first commonly available application. That means more responsive When I talk about threads, I'm talking system to implement this strategy. single-user applications and, just as im­ about concurrency: doing more than one A is a simple flow of control portant, high-performance server appli­ thing at a time. The whole idea is to avoid within a . Its state consists of an cations. Distributed databases that man­ having a processor sit idle when it could instruction pointer, a stack, a register age transactions using threads, rather be doing something useful, and to be set, its priority, and certain types of than entire processes, can be highly effi­ sure that what it is doing is most im­ semaphores. Everything else-memory cient. portant. (i.e., instructions and data), file descrip­ Key factors that determine the perfor~ tors, even the current disk and direc­ Where Are the Applications? mance of a concurrency mechanism are tory-is shared with the other threads in So now I'm back to almost the same the time required to create and switch be­ the process. Threads, like interrupt rou­ question: If everyone knows what the tween tasks and the ease with which tines, require the designer to identify problem is, and if OS/2 provides the tasks can share information. Because critical sections and implement resource­ means of solving it, why don't you see threads carry less state information than sharing protocols. continued

ILLUSTRATION: BETH KROMMES © 1990 SEPTEMBER 1990 • BYTE 101 OS/2 NOTEBOOK OS/2 NOTEBOOK

many great multithreaded OS/2 applica­ thread is a dispatchable element used by books show how to write a multithreaded tions? MS OS/2 to track execution cycles of the program. From discussions I see on BIX To start, building a multithreaded ap­ processor." Three years later, that still and elsewhere, most developers seem to plication takes a tremendous amount of doesn't tell me anything. be gaining the familiarity and exp()rience hard work. Elaborate handshaking is re­ Then came the infamous MTDYNA they need. quired to ensure that threads don't tram­ .DOC. At first, you couldn't use the OS/2 2.0 promises a new, improved ple over each other. Everything has to be library if you wanted to use threads, be­ semaphore application programming in­ reentrant, compiled with the right op­ cause the library routines weren't reen­ terface that's touted as easier to use, al­ tions, and linked with the right libraries. trant. In the spring of 1988, a new release though I'm skeptical. In my experience, Any shared resources have to be sema­ of the C brought the multi­ it's not the semaphore primitives that are phored, and all that semaphoring has to threaded library and headers and a 1039- at fault, it's that semaphores are inher­ be carefully constructed to avoid race line read-me file, MTDYNA.DOC, bur­ ently tricky. Race conditions are just conditions anywhere that could result in ied on one of the disks. For two years, plain tough to avoid and even tougher to "deadly embrace." If the term deadly that was the only official documentation debug. embrace is a bit fuzzy, trust me, writing for most of us. I see nothing that changes that. Some your first multithreaded application will Other roadblocks have been the con­ version 2.0 changes appear to be more give you a good visceral feel for it. stantly changing "musical header files." musical headers. For example, the so­ Whenever a thread needs to "own" I know I've not been alone in just dread­ called FS (fast, safe) semaphores intro­ something, you have to invent a mecha­ ing each new SDK or Toolkit release. duced with great fanfare last year are nism for the purpose. For example, when Each one seemed to bring a new set of gone. What possible reason could there building the , a highly seemingly gratuitous changes to all the have been to introduce these semaphores multithreaded command processor for names defined in the headers. First it at all if they were going to be eliminated OS/2, I had to come up with a way for a was all uppercase, then mixed case; first so quickly? thread to maintain the notion of a current English, then Hungarian. Each release But the biggest impediments to seeing directory. It would hardly have been meant nothing would compile until I'd all those great multithreaded applica­ acceptable if a running quietly in made all the same gratuitous changes to tions now and through the rest of the year the background could suddenly, without all my own source code. will be nontechnical. If sales of OS/2 warning, change the foreground current continue at current levels, don't expect directory. Building a high-performance Least Common Denominator much. mechanism to re-create a current direc­ The other big reason that there aren't Still, there's hope. Although Windows tory notion for each thread turned out to many great multithreaded applications is 3.0 will likely give all of us in the OS/2 be a challenging project. that once you write one, it's not portable. community gas pains, it may ultimately Debugging can be a real treat. Since Conversely, if you're porting something be the best thing that could happen to the kernel's decisions about what thread in from another environment, you don't OS/2. If you have the hardware to run gets to run next depend on what segments just add threads and stir. To really use Windows 3.0 acceptably, OS/2 should are loaded, setting a breakpoint can (by threads, you have to weave them pretty run fine also. OS/2 2.0 will make the mi­ forcing a segment to be loaded) cause a tightly into the fabric of your product. gration easier. I have one DOS box right different execution order. Here's the And let's face it: It's one thing to be non­ now, and, for me, it's one too many, con­ software analog of the hardware bug that portable if you're selling to an installed sidering how often I bother with it. But I disappears when you put the scope probe base of 50 million users, and quite an­ admit even I was strangely captivated to on it. other to a base of only 300,000. see multiple DOS applications like good Not surprisingly, most of the first old Lotus 1-2-3 running in Presentation Not for the Faint of Heart wave of applications for OS/2 have been Manager windows under OS/2 2.0. When I began working on the C shell in ports from DOS or Windows. Micro­ Ultimately, competitive pressures will the summer of 1987, I worried a lot about soft's own Word 5.0 and Excel are two grow as more users and developers learn possible competitors doing the same very disappointing but typical examples just what can be done with threads. Un­ thing (i.e., building Unix-style tools for of programs that do absolutely nothing to like breakfast cereal, where you can eat OS/2). It seemed like an obvious need, take advantage of OS/2. Neither Word the whole box and still have no idea and I knew others were equally capable nor Excel will do background printing; whether it's any good for you, most folks of writing such things. But mostly, that Excel doesn't even let you move its win­ figure out pretty quickly whether new didn't happen. I wondered why. dow around while it prints. software is any good for them. Take One thing that I suspect is that most The arrival of Windows 3.0 clouds heart: OS/2 threads, used properly, are people who did try to build OS/2 applica­ things further. With the upcoming capa­ very good for you-enough so anyone tions came from the DOS world. bility of OS/2 2.0 to run Windows bina­ can notice. • Swamped by the sea change to multitask­ ries unmodified, many developers may ing and multithreading, they had diffi­ think that the right answer is the purely Douglas A. Hamilton is the founder of culty making headway. Time invested opportunistic one: Write it for Windows, Hamilton Laboratories in Wayland, Mas­ with DOS, unless it was spent working and if it works on OS/2, fine, but don't sachusetts, and the author of the Hamil­ on device drivers (which raise acute do anything special. In other words, ton C shell, a command processor and issues of concurrency), isn't good train­ don't use threads. utilities package for OS/2. He can be ing for OS/2 threads. reached on BIX as "hamilton. " Documentation didn't help. I remem­ What's the Prognosis? Your questions and comments are wel­ ber opening my first OS/2 Software De­ In my view, the prognosis is mixed. On come. Write to: Editor, BYTE, One velopment Kit (SDK) and reading that "a the technical side, things have improved. Phoenix Mill Lane, Peterborough, NH continued on page 110 Documentation is much better. Many 03458.