Skip to content

Commit

Permalink
Number of threads spawned depends on hardware available
Browse files Browse the repository at this point in the history
  • Loading branch information
Giuseppe Corbelli committed Oct 9, 2017
1 parent 70b53b1 commit a50e0ab
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// follow with
// strace -o log -ff -tt
#include "../include/stdafx.h"
#include "../include/rtlog/Formatter.hpp"
#include "../include/rtlog/Consumer.hpp"
Expand All @@ -16,6 +18,13 @@ bool is_logged(LogLevel level)
return static_cast<unsigned int>(level) >= loglevel.load(std::memory_order_relaxed);
}

template
fmt::BasicWriter<char>& operator<<(fmt::BasicWriter<char>& os, Argument const& arg);

#if defined(BOOST_LIB_C_GNU) && defined(BOOST_OS_LINUX) && defined(USE_INTERNAL_GETTID)
size_t __thread_id_offset_linux_glibc = rtlog::__find_thread_id_offset();
#endif

} // namespace rtlog


Expand All @@ -31,28 +40,33 @@ int main(int argc, char* argv[])
std::default_random_engine e1(r());
std::uniform_int_distribution<unsigned int> uniform_dist(10, 200);


for (int i{0}; i < 100; i++) {
for (int j{0}; j < 10; j++) {
std::chrono::microseconds sleep_time(uniform_dist(e1));
LOG_INFO("User message", (i*10)+j, sleep_time.count());
std::this_thread::sleep_for(sleep_time);
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
unsigned int max_threads(std::thread::hardware_concurrency());
std::vector<std::future<void>> thread_futures;
for (unsigned int thread_index(0); thread_index < max_threads; thread_index++) {
thread_futures.push_back(
std::move(
std::async(
[&uniform_dist, &e1, thread_index] ()
{
for (int i{0}; i < 100; i++) {
for (int j{0}; j < 10; j++) {
std::chrono::microseconds sleep_time(uniform_dist(e1));
LOG_INFO("Thread idx", thread_index, (i*10)+j, sleep_time.count());
std::this_thread::sleep_for(sleep_time);
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
} // lambda
) // async
) // move
); // push_back
}
consumer.stop();

LOG_INFO("Base thread wait");
for (auto& f : thread_futures)
f.wait();

//~ consumer_thread.join();
//~ std::cout << rtlog::CLogger::get().format();

//~ CFormatter l;
//~ l.write("asd {}", "CCC");
//~ l.format();
//~ l.write("asd {}", -13, 13u);
//~ l.format();
//~ l.write("asd {}");
//~ l.format();
LOG_INFO("Children joined");
consumer.stop();

return 0;
}

0 comments on commit a50e0ab

Please sign in to comment.