Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include "pthread.h"
- using namespace std;
- template <class I>
- struct helper
- {
- void *(I::*method)(void *); //enables you to call and get information from any non static class method
- I *ptr; //this is the class pointer used to call the member pointer
- void *info; //this is what you pass into the member pointer
- };
- template <class T>
- class thread_helper
- {
- public:
- template <class I>
- static T *create()
- {
- T * output=new I;
- return output;
- }
- static void *method(void *in) //This currently is not 100% portable
- {
- //cout<<"I am in here"<<endl;
- helper<T> *utilize = static_cast<helper<T> *>(in);
- return (utilize->ptr->*utilize->method)(utilize->info);
- }
- };
- class test: public thread_helper<test> //need to test with this as a base class
- //with this as virtual and pure virtual
- {
- public:
- virtual void *test_method(void *in)
- {
- int *val= (int *)in;//note: not static_casting since this is not a class
- cout<<"the value is"<<*val<<endl;
- *val *= 4;
- cout<<"the new value is "<< *val<<endl;
- return NULL; //auto casts to void *
- }
- };
- class test_half: public test
- {
- public:
- void *test_method(void *in)
- {
- int *val = (int *)in;
- //cout<<"the value is"<<*val<<endl;
- *val *= 2;
- //cout<<"the new value is" <<*val<<endl;
- return NULL;
- }
- };
- class d_test: public thread_helper<d_test>
- {
- public:
- virtual void *test(void *in)
- {
- cout<<"in d_testn";
- ans = *(double *)in;
- ans *= 2;
- return NULL;
- }
- double ans;
- };
- class d_half: public d_test
- {
- public:
- void *test(void *in)
- {
- ans = *(double *)in;
- ans /= 2.0;
- return NULL;
- }
- };
- class thread_pool
- {
- public:
- thread_pool() : max_threads(2)
- {
- threads.resize(2);
- }
- thread_pool(int num) : max_threads(num)
- {
- threads.resize(max_threads);
- }
- void add_job(void *in)
- {
- jobs.push_back(in);
- }
- template <class I>
- void run_jobs(int start=0, int end=0)
- {
- int tasks;
- //initialize number of tasks to be run
- if (end == 0)
- tasks = jobs.size();
- else //fix bug here
- {
- if (end > jobs.size())
- {
- end = jobs.size();
- }
- tasks = end;
- }
- //initialize starting task
- if (start<0)
- {
- start = 0;
- }
- int task = start;
- //initialize number of threads currently running
- int thread_num = 0;
- //execute thread_pool
- while (task < tasks )
- {
- while (thread_num < max_threads)
- {
- //cout<<"begining thread "<<task<<" with" <<thread_num<<" activen";
- pthread_create(&threads[task], NULL, I::method, jobs[task]);
- //cout<<"finished creating threadn";
- //cout<<"finised thread "<<task<<" thread num is now "<<thread_num<<endl;
- thread_num++;
- task++;
- if (task >= tasks)
- break;
- }
- //join threads
- for (int cnt = task; thread_num > 0; thread_num--, cnt--)
- {
- pthread_join(threads[cnt], NULL);
- }
- }
- }
- private:
- vector<void *> jobs; //Since these pointers are passed to the class
- //they do not need deleting in the class.
- //this class is a temporary owner of these pointers.
- vector<pthread_t> threads;
- int max_threads;
- };
- int main()
- {
- //initialize helper array
- helper<test> first[8];
- thread_pool test_pool(4);
- for (int i = 0; i < 8; i++)
- {
- if (i==0)
- first[0].ptr =test::create<test_half>();
- else
- first[i].ptr = test::create<test>();
- first[i].method = &test::test_method;
- first[i].info = new int(i);
- test_pool.add_job(static_cast<void *>(&first[i]));
- }
- test_pool.run_jobs<test>(0,4);
- for (int i = 0; i<8; i++)
- {
- cout<<"the asnwer is "<< *(int *)first[i].info<<endl;
- }
- //initialize helper array
- helper<d_test> *second=new helper<d_test>[8];
- thread_pool next_pool(4);
- for (int i = 0; i < 8; i++)
- {
- if (i >= 0 && i < 4)
- {
- second[i].ptr = d_test::create<d_test>();
- }
- else
- second[i].ptr = d_test::create<d_half>();
- second[i].method = &d_test::test;
- second[i].info = new double(i);
- next_pool.add_job(static_cast<void *>(&second[i]));
- }
- next_pool.run_jobs<test>(0,4); //this should crash spectacularly
- for (int i=0; i<8; i++)
- cout<<"the answer is "<<second[i].ptr->ans<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement