2010-08-31 18 views

Respuesta

5

productor:

#include <boost/interprocess/managed_shared_memory.hpp> 
#include <boost/interprocess/sync/scoped_lock.hpp> 
#include <boost/interprocess/sync/interprocess_mutex.hpp> 
#include <boost/thread.hpp> 
#include <iostream> 

struct shared_data_t { 
    boost::uint32_t data; 
    boost::interprocess::interprocess_mutex mutex; 
}; 

/***************************************************************************/ 

/* producer */ 
int main(int argc, char** argv) { 
    const char* shname = "_unique_object_name_"; 
    boost::shared_ptr<const char> remover(
     shname, 
     boost::interprocess::shared_memory_object::remove 
    ); 
    try { 
     boost::interprocess::shared_memory_object shared_object(
     boost::interprocess::create_only, 
     shname, 
     boost::interprocess::read_write 
    ); 
     shared_object.truncate(sizeof(shared_data_t)); 
     boost::interprocess::mapped_region region(
     shared_object, 
     boost::interprocess::read_write 
    ); 
     shared_data_t* data = new(region.get_address())shared_data_t; 
     assert(data); 
     const boost::uint32_t count = 0x1000; 
     for (boost::uint32_t idx = 0; idx < count; ++idx) { 
     { boost::interprocess::scoped_lock< 
       boost::interprocess::interprocess_mutex 
      > lock(data->mutex); 
      data->data = idx; 
     } 
     boost::this_thread::sleep(boost::posix_time::seconds(1)); 
     } 
    } catch(boost::interprocess::interprocess_exception &e){ 
     std::cout << e.what() << std::endl; 
     return 1; 
    } 

    return 0; 
} 

consumidor:

#include <boost/interprocess/managed_shared_memory.hpp> 
#include <boost/interprocess/sync/scoped_lock.hpp> 
#include <boost/interprocess/sync/interprocess_mutex.hpp> 
#include <boost/thread.hpp> 

struct shared_data_t { 
    boost::uint32_t data; 
    boost::interprocess::interprocess_mutex mutex; 
}; 

/***************************************************************************/ 

/* consumer */ 
int main(int argc, char** argv) { 
    try { 
     boost::interprocess::shared_memory_object shared_object(
     boost::interprocess::open_only, 
     "_unique_object_name_", 
     boost::interprocess::read_only 
    ); 
     shared_object.truncate(sizeof(shared_data_t)); 
     boost::interprocess::mapped_region region(
     shared_object, 
     boost::interprocess::read_only 
    ); 
     shared_data_t* data = new(region.get_address())shared_data_t; 
     assert(data); 
     while (true) { 
     { boost::interprocess::scoped_lock< 
       boost::interprocess::interprocess_mutex 
      > lock(data->mutex); 
      std::cout << "ping: " << data->data << std::endl; 
     } 
     boost::this_thread::sleep(boost::posix_time::milliseconds(100)); 
     } 
    } catch(boost::interprocess::interprocess_exception &e){ 
     std::cout << e.what() << std::endl; 
     return 1; 
    } 
    return 0; 
} 

hombre: http://www.boost.org/doc/libs/1_43_0/doc/html/interprocess/synchronization_mechanisms.html

Cuestiones relacionadas