Create
This function creates and returns a new thread. The EntryPoint() function will be immediately executed upon creation.
Syntax
- static Thread* Create(Object* EntryPoint(Object* o), Object* o=NULL)
Parameters
- EntryPoint: a pointer to a function for the thread to call.
- o: argument to pass to the EntryPoint function.
Returns
Returns a new thread and immediately begins execution of the specified function.
Example
#include "Leadwerks.h"
using namespace Leadwerks;
Mutex* mutex = NULL;
Object* ThreadFunction(Object* object)
{
for (int i = 0; i < 1000; i++)
{
//Lock the mutex so the two threads dont interfere with each other
mutex->Lock();
System::Print("Thread 2 printing...");
//Unlock the mutex so the other thread can run
mutex->Unlock();
Leadwerks::Time::Delay(1);
}
//Create something to give back to the main thread
Vec3* v = new Vec3(1, 2, 3);
return v;
}
int main(int argc, const char *argv[])
{
//Create a mutex
mutex = Mutex::Create();
//Create a thread. This will launch into the thread function
Thread* thread = Thread::Create(ThreadFunction);
//Wait for the thread to finish
while (thread->GetState() != Thread::Finished)
{
//Lock the mutex so the two threads don't interfere with each other
mutex->Lock();
System::Print("Thread 1 printing...");
//Unlock the mutex so the other thread can run
mutex->Unlock();
Leadwerks::Time::Delay(1);
}
//Get the thread result
Object* result = thread->GetResult();
System::Print(result);
//Cleanup
result->Release();
thread->Release();
mutex->Release();
return 0;
}