/*
    This program demonstrates how to pass multiple arguments to a 
    function that accepts a single void * argument (just like the functions
    used to execute pthread code)

    The basic idea is to create a class that contains all the arguments
    and pass a pointer to the class cast to a void *.
*/

#include <iostream>
using namespace std;

class Foo
{
    public:
        Foo(int a, int b) {m_a = a; m_b = b;}
        int m_a;
        int m_b;
};

void *func(void *arg)
{
    // arg is really a pointer to a Foo
    // cast arg into a Foo *
    // WARNING: when using this trick you must make sure the pointer you
    // are casting (arg) is really the type you are casting to (Foo *)
    Foo *f = (Foo *) arg;

    cout << "f = " << f << "  arg = " << arg << endl;

    cout << "func() f->m_a = " << f->m_a << ", f->m_b = " << f->m_b <<endl;
}

int main()
{
    // Object cannot be a local variable if passing to multiple threads
    Foo *g = new Foo(42, 99);   
    cout << "g = " << g << endl;

    // func() takes a void *, so cast our Foo * to a void *
    func( (void *) g);
}


