fork download
  1. #include <iostream>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <ctime>
  5. #include <atomic>
  6. #include <mutex>
  7.  
  8. // Eager-Initialization, Thread Safe - double check locking with atomic type
  9.  
  10. class current_date_time {
  11.  
  12. private:
  13. //private constuctor and lock variable
  14. current_date_time() {};
  15. static std::mutex m_;
  16.  
  17. public:
  18. //creates global instance on call - saves memory if instance is memory intense
  19. static std::atomic<current_date_time*>& create_instance();
  20. //responsible for assignment of single class object with lock variable
  21. static current_date_time*& initialize_instance(current_date_time*& global_time_variable);
  22.  
  23. void time_output(){
  24. std::time_t t = std::time(nullptr);
  25. std::tm tm = *std::localtime(&t);
  26. std::cout << std::put_time(&tm, "%c %Z") << "\n";
  27. }
  28.  
  29. };
  30.  
  31.  
  32. std::mutex current_date_time::m_;
  33.  
  34. std::atomic<current_date_time*>& current_date_time::create_instance(){
  35. static std::atomic<current_date_time*> global_time_variable = nullptr; //global variable creation
  36. return global_time_variable;
  37. }
  38.  
  39.  
  40.  
  41. current_date_time*& current_date_time::initialize_instance(current_date_time*& global_time_variable){
  42.  
  43. if(global_time_variable == nullptr){ //check to see if the variable is empty
  44. std::lock_guard<std::mutex> lock(m_); //acquire lock
  45. if(global_time_variable == nullptr){ //second check if multiple threads acquire lock
  46. global_time_variable = new current_date_time(); //only one thread executes this line atomically
  47. }
  48. }
  49.  
  50. return global_time_variable;
  51. }
  52.  
  53.  
  54.  
  55. int main() {
  56.  
  57. current_date_time* eager_initialized_variable = current_date_time::create_instance();
  58. current_date_time* after_thread_safe_variable = current_date_time::initialize_instance(eager_initialized_variable);
  59. after_thread_safe_variable->time_output();
  60.  
  61. return 0;
  62. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In static member function ‘static std::atomic<current_date_time*>& current_date_time::create_instance()’:
prog.cpp:35:67: error: use of deleted function ‘std::atomic<_Tp*>::atomic(const std::atomic<_Tp*>&) [with _Tp = current_date_time]’
     static std::atomic<current_date_time*> global_time_variable = nullptr; //global variable creation
                                                                   ^~~~~~~
In file included from prog.cpp:5:0:
/usr/include/c++/6/atomic:328:7: note: declared here
       atomic(const atomic&) = delete;
       ^~~~~~
/usr/include/c++/6/atomic:332:17: note:   after user-defined conversion: constexpr std::atomic<_Tp*>::atomic(std::atomic<_Tp*>::__pointer_type) [with _Tp = current_date_time; std::atomic<_Tp*>::__pointer_type = current_date_time*]
       constexpr atomic(__pointer_type __p) noexcept : _M_b(__p) { }
                 ^~~~~~
stdout
Standard output is empty