把代码写在一个cpp文件里面
std::thread producer(ProducerTask); // 创建生产者线程.
std::thread consumer(ConsumerTask); // 创建消费之线程.
生产者和消费者线程在不同的类中定义:
int TCPCameraBoxing::StartRecv(int port)
{
int nResult = camera_instance.init_socket(port);
if (nResult < 0) {
camera_instance.stop_socket();
return 1;
}
producer_consumer_instance.Initresource(&instance);
b_producer_running = true;
b_consumer_running = true;
t_consumer = std::thread(&TCPCameraBoxing::ConsumerTask, this); // 创建消费之线程.
t_producer = std::thread(&TCPCamera::listen_and_recv, camera_instance); // 创建生产者线程.
return 0;
}