Post by Howard Hin » Sat, 06 Dec 2003 09:08:04

hat is the best way to manage threading in C++ on the Mac?

Pro 9 on the Mac has introduced a new library: Metrowerks::threads,
found in <msl_thread>. This is a thin C++ wrapper over the native OS
threading interface. On Mach-O, Metrowerks::threads relies on
pthreads. On CFM code, Metrowerks::threads calls into Apple's MP
tasks. And soon to be released, the same library will sit on top of
Windows. That is, your application can write to a single threading
interface, but be portable across pthreads, Apple MP tasks and

The Metrowerks::threads lib currently addresses the following issues:

thread creation
thread joining
thread identity
thread sleep/yield
thread local data
exception safe mutex locking/unlocking
recursive mutexes
timed mutexes
condition variables
initialization / call once

See the MSL C++ Reference for full details.

Below is a simple example of a multithreaded program which deals with
a message queue. One thread feeds messages into the back of a queue,
while another thread reads the messages out of the front. The reading
thread will wait (sleep, not poll) if the queue is empty, and the
sending thread will notify the reading thread if it puts a message
into an empty queue.

The main thread simply creates the sending and receiving threads and
then waits for them to complete their tasks. This is all done with a
high level, intuitive interface.

#include <iostream>
#include <queue>
#include <msl_thread>

class unbounded_queue
typedef Metrowerks::mutex Mutex;
typedef Mutex::scoped_lock Lock;

void send (int m);
int receive();

std::queue<int> the_queue_;
Metrowerks::condition queue_is_empty_so_;
Mutex mut_;

unbounded_queue::send (int m)
Lock lock(mut_);
std::cout << "sent: " << m << '\n';
if (the_queue_.size() == 1)

Lock lock(mut_);
while (the_queue_.empty())
int i = the_queue_.front();
std::cout << "received: " << i << '\n';
return i;

unbounded_queue buf;

void sender()
int n = 0;
while (n < 1000)

void receiver()
int n;
n = buf.receive();
} while (n >= 0);

int main()
Metrowerks::thread send(sender);
Metrowerks::thread receive(receiver);

For this example the "message" is simply an int. A negative int marks
the end of the message stream. The sending and receiving threads
write a message to cout to mark the progress of their work.

The main() function simply starts two threads: send and receive. It
then waits for them to complete with the join() function. Only after
both threads have completed will main exit.

The send thread calls the ordinary function sender() which simply
loops, calling the send() member function of a global unbounded_queue
with the messages 0 ... 999, and then finally sends -1 to indicate
that it is done.

The receive thread calls the ordinary function receiver() which loops
on the unbounded_queue's receive() member function until a negative
number is received.

unbounded_queue is a th


Post by Stev » Sun, 07 Dec 2003 04:19:00

On 5/12/03 12:08 am, in article

Soon to be released? I take it this library is already available for Mac
Codewarrior and Windows apps using the x86 cross compiler?

Just not on the Windows compiler?




Post by Howard Hin » Sun, 07 Dec 2003 05:34:47

In article <BBF68AA4.4B731% XXXX@XXXXX.COM >,

That's right. Metrowerks::threads is new with version 9. The latest
Windows hosted version of CodeWarrior is 8.3.



Post by Stev » Sun, 07 Dec 2003 06:11:20

On 5/12/03 8:34 pm, in article

Thanks Howard, I did think that was the situation, just thought I'd check