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

Multithreading 如何在VC+中使用线程和队列+;,multithreading,visual-c++,Multithreading,Visual C++,我想使用两个队列,其中第一个队列将数据推送到队列,第二个线程将从队列中删除数据。 有人能帮我用VC++实现这个吗 我是线程和队列的新手。 这是生产者/消费者问题,这里是C++中的一个/p> < p>这里有几个指针和一些示例代码: std::queue<int> data; // the queue boost::mutex access; // a mutex for synchronising access to the queue boost::condition cond; /

我想使用两个队列,其中第一个队列将数据推送到队列,第二个线程将从队列中删除数据。 有人能帮我用VC++实现这个吗


我是线程和队列的新手。

这是生产者/消费者问题,这里是C++中的一个/p> < p>这里有几个指针和一些示例代码:

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;
}
std::队列数据;//排队
boost::互斥访问;//用于同步对队列的访问的互斥体
boost::条件cond;//用于传递队列状态的条件变量
bool empty()
{
返回data.empty();
}
void thread1()//使用者线程
{
while(true)
{
boost::mutex::作用域锁定(access);
条件等待(锁定,空);
而(!empty())
{
int datum=data.top();
data.pop();
//对数据做点什么
}
}
}
void thread2()//生产者线程
{
while(true)
{
boost::mutex::作用域锁定(access);
data.push_back(1);//由公平掷骰保证随机
第二,通知某人;
}
}
int main()
{
boost::线程t1(线程1);
增压:螺纹t2(螺纹2);
t1.join();
t2.连接();
返回0;
}
我用了一个从和,和从图书馆。boost中的内容与未来的内容基本相同


我只输入了代码,没有办法测试它。你可能会发现虫子。这是一件好事,线程化代码很难,而掌握它的最佳方法是大量实践。

Hi dheer,我的要求是有一个函数,它创建一个线程并将数据传递给进一步的处理,看起来像void MyFunc(char*data){CreateThread(NULL,0,build_data,(void*)data,0,NULL);}现在我想修改上面的代码,我想引入一个队列类并创建两个线程,这样第一个线程可以调用一个函数,将数据写入队列,第二个线程从队列中读取数据。如果需要,请引入互斥。你能告诉我如何在VC++中实现上述功能吗。ThanksHi,我的要求是有一个函数可以创建一个线程,并将数据传递给后续处理,它看起来像void MyFunc(char*data){CreateThread(NULL,0,build_data,(void*)data,0,NULL);}现在我想修改上面的代码我想引入一个队列类并创建两个线程,这样第一个线程可以调用一个函数,将数据写入队列,第二个线程从队列读取数据。如果需要,请引入互斥。你能告诉我如何在VC++中实现上述功能吗。感谢您并非真的很抱歉,您可能应该创建一个新问题,其中包括您当前拥有的所有代码。我真的不明白这个要求