per cpu

 

 

 

#include <iostream>
#include <utility>
#include <thread>
#include <chrono>
#include <functional>
#include <atomic>
#include <vector>
using namespace std;
vector<thread> vthreads;

#define RTE_DEFINE_PER_LCORE(type, name)                        \
                        __thread __typeof__(type) per_lcore_##name
static RTE_DEFINE_PER_LCORE(int, count);
#define RTE_PER_LCORE(name) (per_lcore_##name)
int n=3;
void f1(int n)
{
        RTE_PER_LCORE(count)= 100 + n;
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
        cout <<   RTE_PER_LCORE(count) << endl;
}

void f2(int& n)
{
        RTE_PER_LCORE(count)= 90 + n;
        cout <<   RTE_PER_LCORE(count) << endl;
         std::this_thread::sleep_for(std::chrono::milliseconds(10));
}

int create_thread()
{
    std::thread t2(f1, n + 1); // pass by value
    std::thread t3(f2, std::ref(n)); // pass by reference
    vthreads.push_back(std::move(t2)); 
    vthreads.push_back(std::move(t3)); 
}
int main()
{
    create_thread();
   // for(auto it = vthreads.begin(); it != vthreads.end(); ++it)
   // {
   //                 it->join();
   // }

   for ( auto & th :vthreads)
   {
           th.join();
   }
   std::cout << "Final value of n is " << n << '\n';
   return 0;
}

 

 

root@ubuntu:~/c++# g++ -std=c++11 -pthread per2.cpp -o per2
root@ubuntu:~/c++# ./per2
93
104
Final value of n is 3

 

上一篇:挽救数据库性能的30条黄金法则


下一篇:optimize table tablename