Skip to content

Instantly share code, notes, and snippets.

@AlexsJones
Created August 17, 2012 08:28
Show Gist options
  • Select an option

  • Save AlexsJones/3377010 to your computer and use it in GitHub Desktop.

Select an option

Save AlexsJones/3377010 to your computer and use it in GitHub Desktop.
thread example
#include <iostream>
#include <jnx_headers/jnxlib.hpp>
using namespace std;
using namespace jnx;
class foo
{
public:
void print_args(void);
static bool is_foo_done;
};
bool foo::is_foo_done;
void foo::print_args(void )
{
//grab a handle to our thread args
void *args = jnx_threadmanager::get_thread("thread_one")->get_arg();
//recast them based on type
char *message = reinterpret_cast<char*>(args);
cout << message << endl;
is_foo_done = true;
}
int main(int argc, char **argv) {
//create our thread and push it into the thread pool
jnx_thread *worker_thread = jnx_threadmanager::create_thread("thread_one");
//create our object we wish to run on the seperate thread
foo *bar = new foo();
//set a completion flag to false so that the main doesn't kill the process prematurely
foo::is_foo_done = false;
char *message = "Hello from main!";
//argument is set as a void pointer
worker_thread->set_arg(message);
//start object by binding the memory address against the instance for the start function
worker_thread->start(jnx_bind<foo,&foo::print_args>,bar);
while(foo::is_foo_done != true)
{
sleep(2);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment