This commit is contained in:
gabime 2014-01-27 13:57:52 +02:00
parent 3a30e57d25
commit 0bf2391d4a
2 changed files with 20 additions and 10 deletions

View File

@ -7,9 +7,9 @@ OBJS_DEBUG = $(patsubst %.cpp,debug/%.o,$(_SOURCES))
CXX = g++ CXX = g++
CXXFLAGS = -Wall -std=c++11 -pthread -I../../include CXXFLAGS = -march=native -Wall -std=c++11 -pthread -I../../include
CXX_RELEASE_FLAGS = -O3 -flto CXX_RELEASE_FLAGS = -O2 -flto
CXX_DEBUG_FLAGS= -g CXX_DEBUG_FLAGS= -g -ggdb
OUTLIB_RELEASE = libc11log.a OUTLIB_RELEASE = libc11log.a
OUTLIB_DEBUG = libc11log-debug.a OUTLIB_DEBUG = libc11log-debug.a

View File

@ -27,26 +27,36 @@ int main(int argc, char* argv[])
std::atomic<uint32_t> counter { 0 }; std::atomic<uint32_t> counter { 0 };
auto counter_ptr = &counter; auto counter_ptr = &counter;
std::cout << "Starting " << nthreads << " threads.." << std::endl; std::atomic<bool> active{true};
auto active_ptr = &active;
std::vector<std::thread*> threads;
std::cout << "Starting " << nthreads << " threads for 3 seconds.." << std::endl;
for (int i = 0; i < nthreads; i++) for (int i = 0; i < nthreads; i++)
{ {
new std::thread([&logger, counter_ptr]() { auto t = new std::thread([&logger, counter_ptr, active_ptr]() {
while (true) while (*active_ptr)
{ {
logger.info() << "Hello from thread " << std::this_thread::get_id() << "\tcounter: " << counter_ptr->load(); logger.info() << "Hello from thread " << std::this_thread::get_id() << "\tcounter: " << counter_ptr->load();
(*counter_ptr)++; (*counter_ptr)++;
} }
}); });
threads.push_back(t);
} }
int seconds = 0; int seconds = 0;
while (seconds++ < 5) while (seconds++ < 3)
{ {
counter = 0; counter = 0;
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "Counter = " << utils::format(counter.load()) << std::endl; std::cout << "Counter = " << utils::format(counter.load()) << std::endl;
} }
async->shutdown(std::chrono::seconds(10)); active = false;
for(auto t:threads)
t->join();
async->shutdown(std::chrono::seconds(1));
return 0; return 0;
} }