example_tc_singleton.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "util/tc_singleton.h"
  17. #include "util/tc_timeprovider.h"
  18. #include <unistd.h>
  19. #include <iostream>
  20. using namespace std;
  21. using namespace tars;
  22. class B : public TC_Singleton<B, CreateStatic, PhoneixLifetime>
  23. {
  24. public:
  25. B(){cout << "B" << endl;}
  26. ~B(){cout << "~B" << endl;}
  27. void test(){cout << "test B" << endl;}
  28. };
  29. class A : public TC_Singleton<A, CreateStatic, PhoneixLifetime>
  30. {
  31. public:
  32. A(){cout << "A" << endl;}
  33. ~A()
  34. {
  35. cout << "~A" << endl;
  36. B::getInstance()->test();
  37. }
  38. void test(){cout << "test A" << endl;}
  39. };
  40. int main(int argc, char *argv[])
  41. {
  42. try
  43. {
  44. // A::getInstance()->test();
  45. // B::getInstance()->test();
  46. cout << TC_TimeProvider::getInstance()->getNow() << endl;
  47. sleep(1);
  48. }
  49. catch(exception &ex)
  50. {
  51. cout << ex.what() << endl;
  52. }
  53. return 0;
  54. }