We continue coping with pointers, and our next task is to reference count an array. Let's take a look at the program that gets some data from the stream and processes it in different threads. The code to do this is as follows:
#include <cstring> #include <boost/thread.hpp> #include <boost/bind.hpp> void do_process(const char* data, std::size_t size); void do_process_in_background(const char* data, std::size_t size) { // We need to copy data, because we do not know, // when it will be deallocated by the caller. char* data_cpy = new char[size]; std::memcpy(data_cpy, data, size); // Starting thread of execution to process data. boost::thread(boost::bind(&do_process, data_cpy, size)) .detach(); boost::thread(boost::bind(&do_process, data_cpy, size)) .detach(); // Oops!!! We cannot delete[] data_cpy, because // do_process() function...