zoukankan      html  css  js  c++  java
  • boost::interprocess(2)

    //doc_anonymous_mutex_shared_data.hpp
    #include <boost/interprocess/sync/interprocess_mutex.hpp>
    
    struct shared_memory_log
    {
        enum { NumItems = 100 };
        enum { LineSize = 100 };
    
        shared_memory_log()
            :  current_line(0)
            ,  end_a(false)
            ,  end_b(false)
        {}
    
        //Mutex to protect access to the queue
        boost::interprocess::interprocess_mutex mutex;
    
        //Items to fill
        char   items[NumItems][LineSize];
        int num;
    
        int    current_line;
        bool   end_a;
        bool   end_b;
    };

    发送端:

    #include <boost/interprocess/shared_memory_object.hpp>
    #include <boost/interprocess/mapped_region.hpp>
    #include <boost/interprocess/sync/scoped_lock.hpp>
    #include "doc_anonymous_mutex_shared_data.hpp"
    #include <iostream>
    #include <cstdio>
    #include <windows.h>
    #include <thread>
    using namespace boost::interprocess;
    mapped_region* p_reg;
    
    void funs(shared_memory_log * data)
    {
        while(true)
        {
            {
                //Write some logs
                //Lock the mutex
                scoped_lock<interprocess_mutex> lock(data->mutex);
                /*std::sprintf(data->items[(data->current_line++) % shared_memory_log::NumItems]
                ,"%s_%d", "process_a", i);*/
                data->num++;
                //if(i == (shared_memory_log::NumItems-1))
                //    data->end_a = true;
                //Mutex is released here
                
            }
            Sleep(500);
            //Wait until the other process ends
            /*while(1){
                scoped_lock<interprocess_mutex> lock(data->mutex);
                if(data->end_b)
                    break;
            }*/
        }
    }
    
    int main ()
    {
        try{
            //Remove shared memory on construction and destruction
            //struct shm_remove
            //{
            //    shm_remove() { shared_memory_object::remove("MySharedMemory"); }
            //    ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
            //} remover;
    
            //Create a shared memory object.
            shared_memory_object shm
                (open_or_create               //only create
                ,"MySharedMemory"          //name
                ,read_write   //read-write mode
                );
    
            //Set size
            shm.truncate(sizeof(shared_memory_log));
    
            //Map the whole shared memory in this process
            p_reg = new mapped_region
                (shm                       //What to map
                ,read_write   //Map it as read-write
                );
    
            //Get the address of the mapped region
            void * addr       = p_reg->get_address();
    
            //Construct the shared structure in memory
            shared_memory_log * data = new (addr) shared_memory_log;
    
            std::thread th(funs, data);
            th.detach();
            getchar();
            shared_memory_object::remove("MySharedMemory");
        }
        catch(interprocess_exception &ex){
            std::cout << ex.what() << std::endl;
            shared_memory_object::remove("MySharedMemory");
            getchar();
            return 1;
        }
        return 0;
    }

    接收端:

    #include <boost/interprocess/shared_memory_object.hpp>
    #include <boost/interprocess/mapped_region.hpp>
    #include <boost/interprocess/sync/scoped_lock.hpp>
    #include "doc_anonymous_mutex_shared_data.hpp"
    #include <iostream>
    #include <cstdio>
    #include <thread>
    #include <windows.h>
    using namespace boost::interprocess;
    mapped_region* p_reg;
    int g_num = 0;
    void fung(shared_memory_log * data)
    {
        while (true)
        {
            {
                scoped_lock<interprocess_mutex> lock(data->mutex);
                std::cout << data->num << "---------" << data->num - g_num <<  std::endl;
                g_num = data->num;
            }
            Sleep(300);
    
        }
        
    }
    
    
    int main ()
    {
        //如何保证会删除
        //Remove shared memory on destruction
        //struct shm_remove
        //{
        //    ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
        //} remover;
    
        //Open the shared memory object.
        shared_memory_object shm
            (open_only                  //only open
            ,"MySharedMemory"              //name
            ,read_write  //read-write mode
            );
    
        //Map the whole shared memory in this process
        p_reg = new mapped_region
            (shm                       //What to map
            ,read_write //Map it as read-write
            );
    
        //Get the address of the mapped region
        void * addr       = p_reg->get_address();
    
        //Construct the shared structure in memory
        shared_memory_log * data = static_cast<shared_memory_log*>(addr);
    #if 0
        //Write some logs
        for(int i = 0; i < 100; ++i){
            //Lock the mutex
            scoped_lock<interprocess_mutex> lock(data->mutex);
            std::sprintf(data->items[(data->current_line++) % shared_memory_log::NumItems]
            ,"%s_%d", "process_a", i);
            if(i == (shared_memory_log::NumItems-1))
                data->end_b = true;
            //Mutex is released here
        }
    #endif
        //读log
    #if 1
        std::thread th(fung, data);
        th.detach();
    
    
    #endif
        getchar();
        shared_memory_object::remove("MySharedMemory");
        //Wait until the other process ends
        //while(1){
        //    scoped_lock<interprocess_mutex> lock(data->mutex);
        //    if(data->end_a)
        //        break;
        //}
        return 0;
    }

    有点BUG,在调调

  • 相关阅读:
    【bzoj5180】[Baltic2016]Cities 斯坦纳树
    【BZOJ1859】【ZJOI2006】碗的叠放
    【bzoj4589】Hard Nim FWT+快速幂
    【BZOJ1502】【NOI2005】月下柠檬树 simpson 积分
    【loj6437】 【PKUSC2018】 PKUSC 计算几何
    【PKUSC2018】【loj6433】最大前缀和 状压dp
    【pkuwc2018】 【loj2537】 Minmax DP+线段树合并
    多项式求逆元详解+模板 【洛谷P4238】多项式求逆
    【bzoj3684】 大朋友和多叉树 生成函数+多项式快速幂+拉格朗日反演
    【codeforces 623E】dp+FFT+快速幂
  • 原文地址:https://www.cnblogs.com/zzyoucan/p/3726891.html
Copyright © 2011-2022 走看看