User Tools

Site Tools


c:c_threads:multi_threading

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

c:c_threads:multi_threading [2021/04/28 09:28] – created peterc:c_threads:multi_threading [2021/06/08 09:38] (current) – removed peter
Line 1: Line 1:
-====== C - C++ Threads - Multi threading ====== 
- 
-===== start_thread.hpp ===== 
- 
-<code cpp> 
-#pragma once 
- 
-#include <cstdint> 
- 
-std::int64_t startAndStopThread(int runningMilliseconds, int incrementValue); 
-</code> 
- 
----- 
- 
-===== start_thread.cpp ===== 
- 
-<code 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; 
-} 
-</code> 
  
c/c_threads/multi_threading.1619602116.txt.gz · Last modified: 2021/04/28 09:28 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki