如何在VC ++中使用线程和队列

时间:2022-02-26 01:28:11

I want to use two queues where the 1st queue will push the data to the queue and the 2nd thread will remove the data from the queue. Can somebody help me plz on implementing this in VC++?

我想使用两个队列,其中第一个队列将数据推送到队列,第二个线程将从队列中删除数据。有人可以帮我解决在VC ++中实现这个问题吗?

I am new to the threads and queue.

我是线程和队列的新手。

2 个解决方案

#1


This is the producer / consumer problem, here is one implementation of it in c++

这是生产者/消费者问题,这是c ++中的一个实现

#2


Here is a few pointers and some sample code:

这里有一些指针和一些示例代码:

std::queue<int> data; // the queue
boost::mutex access; // a mutex for synchronising access to the queue
boost::condition cond; // a condition variable for communicating the queue state

bool empty()
{
  return data.empty();
}

void thread1() // the consumer thread
{
  while (true)
  {
    boost::mutex::scoped_lock lock(access);
    cond.wait(lock, empty);
    while (!empty())
    {
      int datum=data.top();
      data.pop();

      // do something with the data
    }
  }
}

void thread2() // the producer thread
{
  while (true)
  {
    boost::mutex::scoped_lock lock(access);
    data.push_back(1); // guaranteed random by a fair dice roll
    cond.notify_one();        
  }
}

int main()
{
  boost::thread t1(thread1);
  boost::thread t2(thread2);
  t1.join();
  t2.join();

  return 0;
}

I used a queue from the STL and threads, condition variables and mutexes from the Boost library. The stuff in boost is pretty much what is going to be in the standard C++ library in a few years so it is good to be familiar with it.

我使用了来自STL的队列以及来自Boost库的线程,条件变量和互斥锁。 boost中的内容几乎就是几年内标准C ++库中的内容,因此熟悉它是很好的。

I only typed the code in, I have no way to test it. You'll probably find bugs. This is a good thing, threaded code is difficult and the best way to get good at it is lots of practice.

我只输入了代码,我无法测试它。你可能会发现错误。这是一件好事,线程代码很难,最好的方法是获得很多实践。

#1


This is the producer / consumer problem, here is one implementation of it in c++

这是生产者/消费者问题,这是c ++中的一个实现

#2


Here is a few pointers and some sample code:

这里有一些指针和一些示例代码:

std::queue<int> data; // the queue
boost::mutex access; // a mutex for synchronising access to the queue
boost::condition cond; // a condition variable for communicating the queue state

bool empty()
{
  return data.empty();
}

void thread1() // the consumer thread
{
  while (true)
  {
    boost::mutex::scoped_lock lock(access);
    cond.wait(lock, empty);
    while (!empty())
    {
      int datum=data.top();
      data.pop();

      // do something with the data
    }
  }
}

void thread2() // the producer thread
{
  while (true)
  {
    boost::mutex::scoped_lock lock(access);
    data.push_back(1); // guaranteed random by a fair dice roll
    cond.notify_one();        
  }
}

int main()
{
  boost::thread t1(thread1);
  boost::thread t2(thread2);
  t1.join();
  t2.join();

  return 0;
}

I used a queue from the STL and threads, condition variables and mutexes from the Boost library. The stuff in boost is pretty much what is going to be in the standard C++ library in a few years so it is good to be familiar with it.

我使用了来自STL的队列以及来自Boost库的线程,条件变量和互斥锁。 boost中的内容几乎就是几年内标准C ++库中的内容,因此熟悉它是很好的。

I only typed the code in, I have no way to test it. You'll probably find bugs. This is a good thing, threaded code is difficult and the best way to get good at it is lots of practice.

我只输入了代码,我无法测试它。你可能会发现错误。这是一件好事,线程代码很难,最好的方法是获得很多实践。