C++ Examples

  1. Write a C++ Program to print a welcome text in a separate line.

C++ thread lambda pass by reference

 #include <iostream>
 #include <thread>
 
 using namespace std;
 
 int main()
 {
 
    // Variable declaration
    
    int x = 4;
    int y = 5;
    
    //Creation of thread with Lambda expression
    
    thread th1([&x, &y]() {
    cout << x <<endl;
    cout << y <<endl;
        
    });
    
    //thread joining
    
    th1.join();
        
    cout << x <<endl;
    cout << y <<endl;
        
    return 0;
  }

Output

4
5
4
5
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments