multithread_fibonacci_call.cpp
Go to the documentation of this file.
00001 #include "ros1_cpptemplate/atomic_fibonacci.hpp"
00002 
00003 #include <sstream>
00004 #include <iostream>
00005 
00006 #include <atomic>
00007 #include <vector>
00008 #include <thread>
00009 #include <chrono>
00010 
00011 #include <ros/console.h>
00012 
00013 using ros1_cpptemplate::AtomicFibonacci;
00014 using ros1_cpptemplate::AtomicFibonacciPtr;
00015 
00016 typedef std::shared_ptr<std::atomic<bool>> FlagPtr;
00017 
00018 int argToInt(char* arg)
00019 {
00020   std::stringstream string_stream;
00021   string_stream << arg;
00022 
00023   int value;
00024   string_stream >> value;
00025 
00026 //   ROS_INFO_STREAM("Converted " << arg << " to " << value);
00027   return value;
00028 }
00029 
00030 void callFibonacci(AtomicFibonacciPtr atomic_fibonacci, std::atomic<bool>& start_flag)
00031 {
00032   while (!start_flag.load())
00033   {
00034     std::chrono::duration<double> sleep_duration(0.001);
00035     std::this_thread::sleep_for(sleep_duration);
00036   }
00037 
00038   atomic_fibonacci->nextAndLog();
00039 }
00040 
00041 int main(int argc, char **argv)
00042 {
00043   int last_number = 0;
00044   int current_number = 1;
00045   int max_number = 256;
00046 
00047   switch (argc)
00048   {
00049     case 1:
00050       break;
00051     case 2:
00052       max_number = argToInt(argv[1]);
00053       break;
00054     case 3:
00055       last_number = argToInt(argv[1]);
00056       current_number = argToInt(argv[2]);
00057       break;
00058     case 4:
00059       last_number = argToInt(argv[1]);
00060       current_number = argToInt(argv[2]);
00061       max_number = argToInt(argv[3]);
00062       break;
00063     default:
00064       ROS_INFO_STREAM("Wrong number of arguments! Max. 3, got " << argc);
00065       return 1;
00066   }
00067 
00068   ROS_INFO("Initializing AtomicFibonacci with: ");
00069   ROS_INFO_STREAM("  last_number    = " << last_number);
00070   ROS_INFO_STREAM("  current_number = " << current_number);
00071   ROS_INFO_STREAM("  max_number     = " << max_number);
00072 
00073   AtomicFibonacciPtr atomic_fibonacci = std::make_shared<AtomicFibonacci>(last_number,
00074                                                                           current_number,
00075                                                                           max_number);
00076 
00077   ROS_INFO_STREAM("Initialized AtomicFibonacci object");
00078 
00079   std::atomic<bool> start_flag;
00080   start_flag = false;
00081 
00082   int number_of_threads = 20;
00083   std::vector<std::thread> threads;
00084 
00085   ROS_INFO_STREAM("Start spawning " << number_of_threads << " threads");
00086 
00087   for (int i = 0; i < number_of_threads; ++i)
00088   {
00089     threads.push_back(std::thread(&callFibonacci, atomic_fibonacci, std::ref(start_flag)));
00090   }
00091 
00092   ROS_INFO_STREAM("Spawning finished, let's go!");
00093 
00094   start_flag = true;
00095 
00096   for (int i = 0; i < threads.size(); ++i)
00097   {
00098     threads[i].join();
00099   }
00100 
00101   ROS_INFO_STREAM("Finished. Goodbye!");
00102 }


ros1_cpptemplate
Author(s): Alexander Reimann
autogenerated on Fri Sep 1 2017 02:23:31