Notes
C Plus Plus
C Plus Plus
  • Roadmap
    • ๐Ÿ›ฃ๏ธC Plus Plus: Docs
  • Build
    • ๐Ÿ”ฅBuild Process
    • ๐Ÿ”งConnect multiple C++ Files
  • Code
    • โฎ๏ธPre-Processors
      • #include
      • #define
      • #ifdef
      • #pragma
      • Predefined Macros
    • LValue and RValue
    • ๐Ÿช…Data Types
      • Enum
      • TypeDef
      • const in c++
      • extern vs inline
    • ๐ŸŽญCasting
    • ๐Ÿ”ƒOperator overloading
      • Available Operators
      • Examples of operator overloading
    • ๐Ÿ—บ๏ธNamespace
      • Namespace Example
      • Using directive
    • ๐Ÿ•ต๏ธHeader File
      • For C++ Classes
    • ๐Ÿ—๏ธStructure
      • Struct vs Class
      • Public vs Private inheritance
    • ๐ŸขClasses
      • Friend Function
      • Copy Constructor
      • Explicit Constructor
      • Move Constructor
        • Move Semantics
      • Other constructors
      • Virtual functions
      • Pure virtual function
      • Other function declaration
      • const function vs final function
  • Memory
    • ๐Ÿง Memory Introduction
    • โœจHeap and Stack
    • ๐ŸŽฏPointers
      • Dangling Pointer
      • 'this' Pointer
      • Function Pointer
      • Smart Pointers
        • Unique Pointer
        • Shared Pointer
        • Weak Pointer
      • Reference count
    • ๐Ÿ‘จโ€๐ŸญHelper function
    • ๐ŸกVector [ArrayList]
      • Custom vector, part 1
      • Custom vector, part 2
      • Custom vector, part 3
      • std::vector
    • โ™ป๏ธUnion
      • Type Punning
      • Type Punning, part 2
      • Type Punning, part 3
      • Union, part 1
      • Union, Part 2
  • Thread
    • ๐ŸงตThreading
      • std::thread
      • Detach a thread
  • Misc
    • ๐Ÿ—‚๏ธExecution Order
    • ๐Ÿง Print memory
Powered by GitBook
On this page
  1. Thread
  2. Threading

std::thread

thread std lib allows us to spawn threads in cpp on demand, join() them or detach() them as per the requirement.

Thread execution is started as soon as it is created with as task.

#include <iostream>
#include <thread>
#include <chrono>
#include <algorithm>
using namespace std;
using namespace std::chrono;
typedef long long int ull;

void findEven(ull start, ull end, ull *EvenSum)
{
    for (ull i = start; i <= end; ++i)
    {
        if (!(i & 1))
        {
            *(EvenSum) += i;
        }
    }
}

void findOdd(ull start, ull end, ull *OddSum)
{
    for (ull i = start; i <= end; ++i)
    {
        if (i & 1)
        {
            (*OddSum) += i;
        }
    }
}

int main()
{
    ull start = 0, end = 1900000000;

    ull OddSum = 0;
    ull EvenSum = 0;

    auto startTime = high_resolution_clock::now();

    // // WITH THREAD
    std::thread t1(findEven, start, end, &(EvenSum));
    std::thread t2(findOdd, start, end, &(OddSum));

    t1.join();
    t2.join();

    // // WITHOUT THREAD
    // findEven(start,end, &EvenSum);
    // findOdd(start, end, &OddSum);
    auto stopTime = high_resolution_clock::now();
    auto duration = duration_cast<microseconds>(stopTime - startTime);

    cout << "OddSum : " << OddSum << endl;
    cout << "EvenSum : " << EvenSum << endl;

    cout << "Sec: " << duration.count() / 1000000 << endl;

    return 0;
}

Try this here.

PreviousThreadingNextDetach a thread

Last updated 8 months ago

๐Ÿงต