ThreadTarget.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // ThreadTarget.h
  3. //
  4. // $Id: ThreadTarget.h 1327 2010-02-09 13:56:31Z obiltschnig $
  5. //
  6. // Library: Foundation
  7. // Package: Threading
  8. // Module: ThreadTarget
  9. //
  10. // Definition of the ThreadTarget class.
  11. //
  12. // Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_ThreadTarget_INCLUDED
  18. #define Foundation_ThreadTarget_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/Runnable.h"
  21. namespace Poco {
  22. class Foundation_API ThreadTarget: public Runnable
  23. /// This adapter simplifies using static member functions as well as
  24. /// standalone functions as targets for threads.
  25. /// Note that it is possible to pass those entities directly to Thread::start().
  26. /// This adapter is provided as a convenience for higher abstraction level
  27. /// scenarios where Runnable abstract class is used.
  28. ///
  29. /// For using a non-static member function as a thread target, please
  30. /// see the RunnableAdapter class.
  31. ///
  32. /// Usage:
  33. /// class MyObject
  34. /// {
  35. /// static void doSomething() {}
  36. /// };
  37. /// ThreadTarget ra(&MyObject::doSomething);
  38. /// Thread thr;
  39. /// thr.start(ra);
  40. ///
  41. /// or:
  42. ///
  43. /// void doSomething() {}
  44. ///
  45. /// ThreadTarget ra(doSomething);
  46. /// Thread thr;
  47. /// thr.start(ra);
  48. {
  49. public:
  50. typedef void (*Callback)();
  51. ThreadTarget(Callback method);
  52. ThreadTarget(const ThreadTarget& te);
  53. ~ThreadTarget();
  54. ThreadTarget& operator = (const ThreadTarget& te);
  55. void run();
  56. private:
  57. ThreadTarget();
  58. Callback _method;
  59. };
  60. //
  61. // inlines
  62. //
  63. inline void ThreadTarget::run()
  64. {
  65. _method();
  66. }
  67. } // namespace Poco
  68. #endif // Foundation_ThreadTarget_INCLUDED