Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2014
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #include "stdafx.hpp"
  2. #include <iostream>
  3. #include <windows.h>
  4. #include <pthread.h>
  5. #include <list>
  6. using namespace std;
  7.  
  8. pthread_mutex_t mutex;
  9. LuaPlus::LuaState* pState = LuaPlus::LuaState::Create( true );
  10.  
  11. struct Task{
  12.   static list<Task*> runningTasks;
  13.   static list<Task*> doneTasks;
  14.   static pthread_mutex_t mutex;
  15.   list<Task>::iterator iterator;
  16.  
  17.   virtual ~Task(){}
  18.   bool start(){
  19.     pthread_mutex_lock(&mutex);
  20.     bool full = runningTasks.size() == 100;
  21.     if(!full){
  22.       runningTasks.push_front(this);
  23.       iterator = runningTasks.begin();
  24.       pthread_t unusedID;
  25.       pthread_create(&unusedID, NULL, &Task::threadBody, this);
  26.     }
  27.     pthread_mutex_unlock(&mutex);
  28.     return full;
  29.   }
  30.   virtual void run() = 0;
  31.   virtual void processResults() = 0;
  32.  
  33. protected:
  34.   void finish(){
  35.     pthread_mutex_lock(&mutex);
  36.     runningTasks.erase(iterator);
  37.     doneTasks.push_front(this);
  38.     pthread_mutex_unlock(&mutex);
  39.   }
  40.   static void* threadBody(void* instance){
  41.     Task* task = instance;
  42.     task->run();
  43.     task->finish();
  44.     return NULL;
  45.   }
  46. };
  47.  
  48. struct SumTask: public Task{
  49.   int a, b, c;
  50.   void run(){
  51.     Sleep(3000);
  52.     c = a+b;
  53.   }
  54.   void processResults(){
  55.     LuaPlus::LuaFunction<int> CPP_OnMyEvent = pState->GetGlobal("LUA_OnMyEvent");
  56.     CPP_OnMyEvent(a,b,c);
  57.   }
  58. };
  59.  
  60. //functions called by Lua
  61. bool runSumTask(int a, int b){
  62.   SumTask *task = new SumTask();
  63.   task->a = a; task->b = b;
  64.   bool ok = task->start();
  65.   if(!ok)
  66.     delete task;
  67.   return ok;
  68. }
  69.  
  70. int gatherResults(){
  71.   pthread_mutex_lock(&Task::mutex);
  72.   int totalResults = Task::doneTasks.size();
  73.   while(Task::doneTasks.size() > 0){
  74.     Task* t = Task::doneTasks.front();
  75.     Task::doneTasks.pop_front();
  76.     t->processResults();
  77.     delete t;
  78.   }
  79.   pthread_mutex_unlock(&Task::mutex);
  80.   return totalResults;
  81. }
  82.  
  83. int main() {
  84.     pthread_mutex_init(&mutex, NULL); //initialize the mutex
  85.     LuaPlus::LuaObject globals = pState->GetGlobals();
  86.    
  87.     globals.RegisterDirect("runSumTask", runSumTask);
  88.     globals.RegisterDirect("gatherResults", gatherResults);
  89.  
  90.     char pPath[ MAX_PATH ];
  91.     GetCurrentDirectory(MAX_PATH,pPath);
  92.     strcat_s(pPath,MAX_PATH,"\\main.lua");
  93.     if( pState->DoFile(pPath) ) { //run our main.lua script which contains the callback function that will run a print
  94.         if( pState->GetTop() == 1 )
  95.             std::cout << "An error occured: " << pState->CheckString(1) << std::endl;
  96.     }
  97.  
  98.     //clean up
  99.     LuaPlus::LuaState::Destroy( pState );
  100.     pState = nullptr;
  101.     pthread_mutex_destroy(&mutex);
  102.     getchar(); //keep console from closing
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement