code_junkie

Passing function Pointers in C++

Nov 13th, 2011
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3.  
  4.  
  5. void printSome (int i)
  6. {
  7. std::cout << i << std::endl;
  8. }
  9.  
  10. void spawnThread (void (*threadName)(int i))
  11. {
  12. CreateThread
  13. (
  14. 0, // default security attributes
  15. 0, // use default stack size
  16. (LPTHREAD_START_ROUTINE)threadName, // thread function name
  17. (LPVOID)i, // argument to thread function
  18. 0, // use default creation flags
  19. 0 // returns the thread identifier
  20. );
  21. }
  22.  
  23. int main ()
  24. {
  25. spawnThread(printSome(155));
  26. }
  27.  
  28. class print_some : public basic_thread {
  29. private:
  30. int i;
  31. public:
  32. print_some (int i) : i(i) {};
  33. action_callback () {
  34. std::cout << i << std::endl;
  35. }
  36. }
  37. int main () {
  38. print_some printer (155);
  39. }
  40.  
  41. class basic_thread :
  42. {
  43. public:
  44. basic_thread();
  45. protected:
  46. unsigned long m_ThreadId;
  47.  
  48. virtual void action_callback () {};
  49.  
  50. // Internal routine used to bridge between OS callback format and
  51. // action_callback. *Must* be static for the OS.
  52. static unsigned long __stdcall self_calling_callback (void *parameter);
  53. }
  54.  
  55. unsigned long __stdcall basic_thread::self_calling_callback (void *parameter) {
  56. if (parameter) {
  57. basic_thread * thread = reinterpret_cast<basic_thread *>(parameter);
  58. thread->action_callback();
  59. }
  60. return 0; // The value returned only matters if someone starts calling GetExitCodeThread
  61. // to retrieve it.
  62. }
  63.  
  64. basic_thread::basic_thread () {
  65. // Start thread.
  66. m_Handle = CreateThread(NULL,
  67. 0,
  68. self_calling_callback,
  69. (PVOID)this,
  70. 0,
  71. &m_ThreadId );
  72. if( !IsHandleValid() )
  73. throw StartException("CreateThread() failed", GetLastError());
  74.  
  75. }
  76.  
  77. void spawnThread(void (*threadEntryPoint)(int), int argument)
  78. {
  79. CreateThread(0,0,
  80. (LPTHREAD_START_ROUTINE)threadEntryPoint,
  81. (LPVOID)argument,
  82. 0,0);
  83. }
  84.  
  85. spawnThread(printSome, 155);
  86.  
  87. #include <iostream>
  88. #include <process.h>
  89. #include <windows.h>
  90.  
  91. void
  92. printSome(int i)
  93. {
  94. std::cout << i << std::endl;
  95. }
  96.  
  97. HANDLE
  98. spawnThread(void (*threadName)(int), int i)
  99. {
  100. return (HANDLE) _beginthread((void (*)(void*)) threadName, 0, (LPVOID) i);
  101. }
  102.  
  103. int
  104. main(int argc, char *argv[])
  105. {
  106. HANDLE threadHandle;
  107.  
  108. threadHandle = spawnThread(printSome, 155);
  109. WaitForSingleObject(threadHandle, INFINITE);
  110.  
  111. return 0;
  112. }
  113.  
  114. #include <iostream>
  115. #include <process.h>
  116. #include <windows.h>
  117.  
  118. class Thread {
  119. static void proxy(void *arg) { (*(reinterpret_cast<Thread *> (arg)))(); }
  120. HANDLE thread_;
  121.  
  122. public:
  123. virtual ~Thread() {}
  124. virtual void operator()() = 0;
  125. void start() { thread_ = (HANDLE) _beginthread(Thread::proxy, 0, this);}
  126. void waitForExit() { WaitForSingleObject(thread_, INFINITE); }
  127. };
  128.  
  129. class Printer : public Thread {
  130. int i_;
  131.  
  132. public:
  133. Printer(int i) : i_(i) {}
  134. void operator()() { std::cout << i_ << std::endl; }
  135. };
  136.  
  137. int
  138. main(int argc, char *argv[])
  139. {
  140. Printer p(155);
  141.  
  142. p.start();
  143. p.waitForExit();
  144.  
  145. return 0;
  146. }
  147.  
  148. spawnThread(printSome(155));
  149.  
  150. //------------------------------------------------------------------------------------
  151. // 2.6 How to Pass a Function Pointer
  152.  
  153. // <pt2Func> is a pointer to a function which returns an int and takes a float and two char
  154. void PassPtr(int (*pt2Func)(float, char, char))
  155. {
  156. int result = (*pt2Func)(12, 'a', 'b'); // call using function pointer
  157. cout << result << endl;
  158. }
  159.  
  160. // execute example code - 'DoIt' is a suitable function like defined above in 2.1-4
  161. void Pass_A_Function_Pointer()
  162. {
  163. cout << endl << "Executing 'Pass_A_Function_Pointer'" << endl;
  164. PassPtr(&DoIt);
  165. }
Add Comment
Please, Sign In to add comment