Guest User

Untitled

a guest
Mar 6th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. =begin
  2.  
  3. The purpose of this code was to write a little practice program that would enable me to generate a thread safe way of writing strings to a logfile. I would also like to use the Queue with my threads. I read that it was thread safe in my handbook. The reason why this is helpful is because it is more efficient to buffer a fixed number of reports before writing it to a file rather than continuously writing to a file every time a single string is received. As you can see, I haven't even gotten to a point where I could write to a file. For some reason, I never seem to get the first thread inside of the synchronize method inside of thread_queuetest(). I am open to any suggestions. Sorry if this is pretty complicated.
  4.  
  5. =end
  6.  
  7.  
  8. require 'thread'
  9. require 'monitor'
  10.  
  11. class ReportManagement
  12.  
  13. def Initialize()
  14.  
  15. @queuemax = 256
  16. @queue = queue.new
  17. @queue.extend(MonitorMixin)
  18. @maxed_reports_cond = @queue.new_cond
  19. @shutdown_ok = false
  20. end
  21.  
  22. def ts_reportlogs(outfile)
  23.  
  24. if(outfile.class != File)
  25.  
  26. print "invalid arguments passed into ReportManagement::ts_reportlogs()\n"
  27. exit
  28. end
  29.  
  30. report_thread = Thread.new(outfile) {
  31. loop do
  32. @queue.synchronize do
  33. @maxed_reports_cond.wait_while {@queue.empty?}
  34. res = @queue.pop
  35. print "[!] Report #{Thread.current} dequeued #{res} from report buffer.\n"
  36. end
  37. end
  38. }
  39. end
  40.  
  41. def self.open_logfile(filename=nil)
  42. filename ||= get_filename_from_time
  43. outfile = File.new(filename, File::WRONLY|File::CREAT|File::APPEND)
  44. return outfile
  45. end
  46. def thread_queuetest()
  47.  
  48. threads = []
  49.  
  50. 1000.times do |x|
  51. threads << Thread.new(x) {
  52.  
  53. @queue.synchronize do
  54. if(@shutdown_ok && queue.empty?)
  55. break
  56. elsif(@queue.length >= @queuemax)
  57. @maxed_reports_cond.signal
  58. @queue.wait_while {@queue.length >= @queuemax}
  59. end
  60. @queue.enq("\"#{x} hello from #{Thread.current}\"")
  61. end
  62. }
  63. end
  64. threads.join
  65. end
  66.  
  67. protected
  68. def self.get_filename_from_time
  69. t = Time.now()
  70. filename = t.strftime("%Y.%m.%d.%H.%M.%S.log")
  71. end
  72. end
  73.  
  74. # instantiate the object that will manage a thread safe logging system
  75. myobject = ReportManagement.new
  76.  
  77. # generate an output file for logging
  78. outfile = ReportManagement.open_logfile()
  79.  
  80. # populate a queue with simulated results from multithreaded tasks
  81. myqueue = myobject.thread_queuetest()
  82.  
  83. # This is where we do the real work - write results to a file...
  84. # in a threadsafe manner using mixins (signals/cond variables)
  85. myobject.ts_reportlogs(outfile)
  86. @shutdown_ok = true
Add Comment
Please, Sign In to add comment