Advertisement
Guest User

Untitled

a guest
Aug 28th, 2014
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include "pthread.h"
  4.  
  5. using namespace std;
  6.  
  7. template <class I>
  8. struct helper
  9. {
  10. void *(I::*method)(void *); //enables you to call and get information from any non static class method
  11. I *ptr; //this is the class pointer used to call the member pointer
  12. void *info; //this is what you pass into the member pointer
  13. };
  14.  
  15. template <class T>
  16. class thread_helper
  17. {
  18. public:
  19. template <class I>
  20. static T *create()
  21. {
  22. T * output=new I;
  23. return output;
  24. }
  25.  
  26. static void *method(void *in) //This currently is not 100% portable
  27. {
  28. //cout<<"I am in here"<<endl;
  29. helper<T> *utilize = static_cast<helper<T> *>(in);
  30. return (utilize->ptr->*utilize->method)(utilize->info);
  31. }
  32. };
  33.  
  34. class test: public thread_helper<test> //need to test with this as a base class
  35. //with this as virtual and pure virtual
  36. {
  37. public:
  38. virtual void *test_method(void *in)
  39. {
  40. int *val= (int *)in;//note: not static_casting since this is not a class
  41. cout<<"the value is"<<*val<<endl;
  42. *val *= 4;
  43. cout<<"the new value is "<< *val<<endl;
  44. return NULL; //auto casts to void *
  45. }
  46. };
  47.  
  48. class test_half: public test
  49. {
  50. public:
  51. void *test_method(void *in)
  52. {
  53. int *val = (int *)in;
  54. //cout<<"the value is"<<*val<<endl;
  55. *val *= 2;
  56. //cout<<"the new value is" <<*val<<endl;
  57. return NULL;
  58. }
  59. };
  60.  
  61. class d_test: public thread_helper<d_test>
  62. {
  63. public:
  64. virtual void *test(void *in)
  65. {
  66. cout<<"in d_testn";
  67. ans = *(double *)in;
  68. ans *= 2;
  69. return NULL;
  70. }
  71. double ans;
  72. };
  73.  
  74. class d_half: public d_test
  75. {
  76. public:
  77. void *test(void *in)
  78. {
  79. ans = *(double *)in;
  80. ans /= 2.0;
  81. return NULL;
  82. }
  83. };
  84.  
  85. class thread_pool
  86. {
  87. public:
  88. thread_pool() : max_threads(2)
  89. {
  90. threads.resize(2);
  91. }
  92. thread_pool(int num) : max_threads(num)
  93. {
  94. threads.resize(max_threads);
  95. }
  96. void add_job(void *in)
  97. {
  98. jobs.push_back(in);
  99. }
  100. template <class I>
  101. void run_jobs(int start=0, int end=0)
  102. {
  103. int tasks;
  104. //initialize number of tasks to be run
  105. if (end == 0)
  106. tasks = jobs.size();
  107. else //fix bug here
  108. {
  109. if (end > jobs.size())
  110. {
  111. end = jobs.size();
  112. }
  113. tasks = end;
  114. }
  115. //initialize starting task
  116. if (start<0)
  117. {
  118. start = 0;
  119. }
  120. int task = start;
  121. //initialize number of threads currently running
  122. int thread_num = 0;
  123. //execute thread_pool
  124. while (task < tasks )
  125. {
  126. while (thread_num < max_threads)
  127. {
  128. //cout<<"begining thread "<<task<<" with" <<thread_num<<" activen";
  129. pthread_create(&threads[task], NULL, I::method, jobs[task]);
  130. //cout<<"finished creating threadn";
  131. //cout<<"finised thread "<<task<<" thread num is now "<<thread_num<<endl;
  132. thread_num++;
  133. task++;
  134. if (task >= tasks)
  135. break;
  136. }
  137. //join threads
  138. for (int cnt = task; thread_num > 0; thread_num--, cnt--)
  139. {
  140. pthread_join(threads[cnt], NULL);
  141. }
  142. }
  143. }
  144. private:
  145. vector<void *> jobs; //Since these pointers are passed to the class
  146. //they do not need deleting in the class.
  147. //this class is a temporary owner of these pointers.
  148. vector<pthread_t> threads;
  149. int max_threads;
  150. };
  151.  
  152. int main()
  153. {
  154. //initialize helper array
  155. helper<test> first[8];
  156. thread_pool test_pool(4);
  157. for (int i = 0; i < 8; i++)
  158. {
  159. if (i==0)
  160. first[0].ptr =test::create<test_half>();
  161. else
  162. first[i].ptr = test::create<test>();
  163. first[i].method = &test::test_method;
  164. first[i].info = new int(i);
  165. test_pool.add_job(static_cast<void *>(&first[i]));
  166. }
  167.  
  168. test_pool.run_jobs<test>(0,4);
  169. for (int i = 0; i<8; i++)
  170. {
  171. cout<<"the asnwer is "<< *(int *)first[i].info<<endl;
  172. }
  173.  
  174. //initialize helper array
  175. helper<d_test> *second=new helper<d_test>[8];
  176. thread_pool next_pool(4);
  177. for (int i = 0; i < 8; i++)
  178. {
  179. if (i >= 0 && i < 4)
  180. {
  181. second[i].ptr = d_test::create<d_test>();
  182. }
  183. else
  184. second[i].ptr = d_test::create<d_half>();
  185. second[i].method = &d_test::test;
  186. second[i].info = new double(i);
  187. next_pool.add_job(static_cast<void *>(&second[i]));
  188. }
  189.  
  190. next_pool.run_jobs<test>(0,4); //this should crash spectacularly
  191. for (int i=0; i<8; i++)
  192. cout<<"the answer is "<<second[i].ptr->ans<<endl;
  193. return 0;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement