// ---------------------------------------------------------------------------
// 
//   Lily Engine alpha
// 
//   Copyright (c) 2010 Clifford Jolly
//     http://cliffjolly.com
//     cliff@cliffjolly.com
// 
// ---------------------------------------------------------------------------
// 
//   This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation; either version 2 of the License, or
//   (at your option) any later version.
//  
//   This program is distributed in the hope that it will be useful, but
//   WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//   General Public License for more details.
// 
//   You should have received a copy of the GNU General Public License
//   along with this program; if not, write to the Free Software
//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
//   USA
// -------------------------- END HEADER -------------------------------------

#include <boost/bind.hpp>

#include <string>
#include <vector>
#include <iostream>
using namespace std;

#include <time.h>

#include "thread.h"
using namespace Threads;

Mutex mainMut;
Mutex outputMut;

void bar(int a, int b, int c, string blargh, vector<string> *mainVec) {

    cout << "FUCK" << endl;

    outputMut.lock();
    cout << "Thread started: " << getMyId() << " with string " << blargh << endl;
    outputMut.unlock();

    cout << "SHIT" << endl;

    cout << a << b << c << blargh << endl;

    cout << "PISS" << endl;

    for(int i = 0; i < 10; i++) {
        mainMut.lock();
        mainVec->push_back(blargh);
        mainMut.unlock();
    }

    cout << "DAMN" << endl;

/*#ifdef WIN32
    Sleep(50);
    #endif*/

}

// HEY CLIFF READ THIS.
// Something is causing the threads to keep going after a join. Look at this ouput...

// Main thread: 51
// FUCK
// FUCK
// FUCK
// JOINING EVERYTHING
// Thread started: 50 with string DER
// Thread started: 48 with string Moo
// SHIT
// 789DER
// PISS
// Thread started: 49 with string Ugh
// SHIT
// 123Moo
// PISS
// DAMN
// DAMN
// OUTPUT                                // Everything should have joined by here.
// DER
// DER
// DER
// DER
// DER
// Moo
// Moo
// Moo
// Moo
// SHIT                                  // So why is THIS (the "Ugh" thread) still going down here?
// 456Ugh
// PISS
// Mowine: Unhandled exception 0xc0000417 at address 0x40815a (thread 0033), starting debugger...


int main(int argc, char *argv[]) {

    vector<string> mainVec;

    cout << "Main thread: " << Threads::getMyId() << endl;

    Thread gt(boost::bind(bar, 7, 8, 9, "DER", &mainVec));
    Thread foo = Thread(boost::bind(bar, 1, 2, 3, "Moo", &mainVec));
    Thread guh = Thread(boost::bind(bar, 4, 5, 6, "Ugh", &mainVec));

    outputMut.lock();
    
    //cout << "Thread IDs from the main thread: " << foo.getId() << " " << guh.getId() << endl;

    outputMut.unlock();

    cout << "JOINING EVERYTHING" << endl;

    foo.join();
    guh.join();
    gt.join();

    cout << "OUTPUT" << endl;

    for(unsigned int i = 0; i < mainVec.size(); i++) {
        cout << mainVec[i] << endl;
    }

    cout << "DONE" << endl;

    return 0;
}
