Advertisement
Guest User

Untitled

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