c:c_threads:multi_threading
This is an old revision of the document!
Table of Contents
C - C++ Threads - Multi threading
start_thread.hpp
#pragma once #include <cstdint> std::int64_t startAndStopThread(int runningMilliseconds, int incrementValue);
start_thread.cpp
#include "start_thread.hpp" #include <atomic> #include <thread> class SampleThreadClass { public: void threadFunction(int inputVal) { while (running) { result += inputVal; } } std::atomic<bool> running{true}; int64_t result{0}; }; int64_t startAndStopThread(int runningMilliseconds, int incrementValue) { SampleThreadClass sampleThreadClass; // Start thread. std::thread t = std::thread(&SampleThreadClass::threadFunction, &sampleThreadClass, incrementValue); // Wait. std::this_thread::sleep_for(std::chrono::milliseconds(runningMilliseconds)); // Stop thread. sampleThreadClass.running = false; t.join(); return sampleThreadClass.result; }
c/c_threads/multi_threading.1619602116.txt.gz ยท Last modified: 2021/04/28 09:28 by peter