mr-edd.co.uk :: horsing around with the C++ programming language

coco

coco is a library that implements the Boost.Thread interface but is written in terms of the operating system’s native cooperative threading mechanism, whether that be Fibers on Windows, or the <ucontext.h> functions on POSIX machines.

This means that multi-threaded programs written using Boost.Thread can now be run in a single thread. This is useful for debugging and testing purposes, where it is necessary to look at algorithmic correctness in isolation from correctness of synchronisation.

Recent activity

Code

Clone the repository using mercurial:

> hg clone http://bitbucket.org/edd/coco

Or get a zip file of the code.

Quick start

Currently, coco mirrors version 1.34 of the boost threads API. I hope to update it to match the latest boost threads code soon.

To use coco:

  1. tell your compiler to look at coco's fake boost thread headers before the real boost headers
  2. link against coco instead of the boost threads library

Alternatively, by #include-ing coco's headers rather than the boost substitutes, the threading functionality is exposed in the coco namespace:

#include <coco/thread.hpp>
#include <iostream>

void hello()
{
    std::cout << "hello\n";
}

int main()
{
    coco::thread t(&hello);
    t.join();
    return 0;
}

Further reading

Comments

George Yohng

[12/10/2008 at 19:58:00]

Hello!

I have seen another C++ cooperative threading library under the name of coco. Does it by any chance related to this one?

http://luajit.org/coco.html

Thanks,
George.

Edd

[13/10/2008 at 17:22:00]

Hi George,

No that library is no relation, though they do the same thing, very roughly speaking. The other coco is written in C for lua, rather than C++ for C++.

Rydinare

[18/10/2008 at 21:29:00]

Awesome library. I had seen this a while ago, but now I'm using it for the first time. I inserted into one of my unit tests and it helped me track down a race condition that was unclear before this. I think this is a fantastic tool, particularly for debugging. I just wanted to write and say thanks, since traditionally, unit testing multi-threaded code can be a pain, but the use of fibers is brilliant, because it removes the non-determinism.

Awesome work.

Edd

[19/10/2008 at 13:21:00]

That's always nice to hear! Thanks.

(optional)
(optional)
(required, hint)

Links can be added like [this one -> http://www.mr-edd.co.uk], to my homepage.
Phrases and blocks of code can be enclosed in {{{triple braces}}}.
Any HTML markup will be escaped.