| 138 | // ---------------------------------------------------------------------------------------- |
| 139 | |
| 140 | int main() |
| 141 | { |
| 142 | // Set the dlog object so that it logs everything. |
| 143 | dlog.set_level(LALL); |
| 144 | |
| 145 | pipe_example pe; |
| 146 | |
| 147 | for (int i = 0; i < 15; ++i) |
| 148 | { |
| 149 | dlog << LINFO << "Add job " << i << " to pipe"; |
| 150 | job j; |
| 151 | j.id = i; |
| 152 | |
| 153 | |
| 154 | // Add this job to the pipe. One of our three threads will get it and process it. |
| 155 | // It should also be pointed out that the enqueue() function uses the global |
| 156 | // swap function to move jobs into the pipe. This means that it modifies the |
| 157 | // jobs we are passing in to it. This allows you to implement a fast swap |
| 158 | // operator for your jobs. For example, std::vector objects have a global |
| 159 | // swap and it can execute in constant time by just swapping pointers inside |
| 160 | // std::vector. This means that the dlib::pipe is effectively a zero-copy |
| 161 | // message passing system if you setup global swap for your jobs. |
| 162 | pe.job_pipe.enqueue(j); |
| 163 | } |
| 164 | |
| 165 | dlog << LINFO << "main ending"; |
| 166 | |
| 167 | // the main function won't really terminate here. It will call the destructor for pe |
| 168 | // which will block until all the jobs have been processed. |
| 169 | } |
| 170 | |
| 171 | // ---------------------------------------------------------------------------------------- |
| 172 | |