• 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 << '
    ';
       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
    #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;
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            cout <<   RTE_PER_LCORE(count) << endl;
    }
    
    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 << '
    ';
       return 0;
    }
    root@ubuntu:~/c++# g++ -std=c++11 -pthread per2.cpp -o per2
    root@ubuntu:~/c++# ./per2
    104
    93
    Final value of n is 3
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <time.h>
    #include <unistd.h>
    
    __thread int var = 8;
    
    void *func0(void *arg){
            ++var;
            printf("func0:%d
    ", var);
    }
    
    void *func1(void *arg){
            usleep(3);
            ++var;
            printf("func1:%d
    ", var);
    }
    
    
    int main(){
    
            pthread_t p0, p1;
            int i=0;
            pthread_create(&p0, NULL,  func0, NULL);
            pthread_create(&p1, NULL,  func1, NULL);
    
            pthread_join(p0, NULL);
            pthread_join(p1, NULL);
            return 0;
    }
    root@ubuntu:~/dpdk-function# g++ -pthread  -o thread  thread.c 
    root@ubuntu:~/dpdk-function# ./thread
    func0:9
    func1:9
  • 相关阅读:
    bootstrap
    Bootstrap面试题
    h5+css3 考试题
    less安装与使用
    css3(四)响应式 Web 设计 - 媒体查询
    css3(三)弹性布局
    css3(三)多列布局
    JavaScriptAPI初识
    Bootstrap面试题
    BS初识
  • 原文地址:https://www.cnblogs.com/dream397/p/14710534.html
Copyright © 2020-2023  润新知