zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    一个可以代替冗长switch-case的消息分发小框架
    [JCIP笔记](五)JDK并发包
    [JCIP笔记](四)踩在巨人的肩上
    [JCIP笔记] (三)如何设计一个线程安全的对象
    工作两年的五个感想
    [JCIP笔记] (二)当我们谈线程安全时,我们在谈论什么
    [JCIP笔记] (一)多线程的起源
    每天进步一点点------CORDIC (一)
    每天进步一点点------Alpha半透明图形叠加算法Matlab+Verilog实现
    每天进步一点点------Altium Designer Rules规则详解
  • 原文地址:https://www.cnblogs.com/dream397/p/14710534.html
Copyright © 2011-2022 走看看