Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 多线程和使用事件_C++_Multithreading_Winapi_Events_Thread Safety - Fatal编程技术网

C++ 多线程和使用事件

C++ 多线程和使用事件,c++,multithreading,winapi,events,thread-safety,C++,Multithreading,Winapi,Events,Thread Safety,我的程序有三个线程,我正在努力学习同步和线程安全。下面我将概述不同线程的作用,但我想学习如何使用事件来触发不同线程中的每个进程,而不是无限读取(这给了我并发性问题) 谷歌提供了很多选择,但我不确定在这种情况下什么是最好的实现方式——你能指出一个标准方法/事件的方向,我可以学习如何最好地实现它吗 我在VS 2012上这样做,理想情况下我不会使用外部库,例如boost 线程1:接收消息并将其推送到全局队列中,queue msg\u in 线程2:在无限循环上(即而(1));等待如果(!msg_in.

我的程序有三个线程,我正在努力学习同步和线程安全。下面我将概述不同线程的作用,但我想学习如何使用事件来触发不同线程中的每个进程,而不是无限读取(这给了我并发性问题)

谷歌提供了很多选择,但我不确定在这种情况下什么是最好的实现方式——你能指出一个标准方法/事件的方向,我可以学习如何最好地实现它吗

我在VS 2012上这样做,理想情况下我不会使用外部库,例如boost

线程1:接收消息并将其推送到全局队列中,
queue msg\u in

线程2:在无限循环上(即
而(1)
);等待
如果(!msg_in.empty())
执行一些处理,并将其推送到全局
映射msg_out

while (1)
{
    if (!msg_in.empty())
    {
        //processes 
        msg_map[i][j].push(); //i and j are int (irrelevant here)
    }

}
线程3:

while (1)
{
    if (msg_map.find(i) != msg_map.end())
    {
        if (!msg_map[i].find(j)->second.empty())
        {
            //processes 
        }
    }
}

你的问题是生产者和消费者的问题。您可以对事件使用条件变量。这里有一个例子:

如果你需要的话,我已经把它改编成你的例子

#include "MainThread.h"


#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include <atomic>
#include <condition_variable>

std::mutex m;
std::condition_variable cv;
bool ready = false;
bool processed = false;

void worker_thread(unsigned int threadNum)
{
    // Wait until main() sends data
    {
        std::unique_lock<std::mutex> lk(m);
        cv.wait(lk, []{return ready;});
    }

    std::cout << "Worker thread "<<threadNum <<" is processing data"<<std::endl;

    // Send data back to main()
    {
        std::lock_guard<std::mutex> lk(m);
        processed = true;
        std::cout << "Worker thread "<< threadNum <<" signals data processing completed\n";
    }
    cv.notify_one();
}


int initializeData()
{
    // send data to the worker thread
    {
        std::lock_guard<std::mutex> lk(m);
        ready = true;
        std::cout << "Data initialized"<<std::endl;
    }
    cv.notify_one();
    return 0;
}

int consumerThread(unsigned int nbThreads)
{
    std::atomic<unsigned int> nbConsumedthreads=0;
    while (nbConsumedthreads<nbThreads)
    {
        std::unique_lock<std::mutex> lk(m);
        cv.wait(lk, []{return processed;});
        std::cout<<"Data processed counter="<<nbConsumedthreads << " "<<  std::endl;
        ++nbConsumedthreads;
        cv.notify_one();
    }

    return 0;
}

int main()
{
    const unsigned int nbThreads=3;
    std::thread worker1(worker_thread,1);
    std::thread worker2(worker_thread,2);
    std::thread worker3(worker_thread,3);

    std::thread init(initializeData);

    std::thread consume(consumerThread, nbThreads);



    worker1.join();
    worker2.join();
    worker3.join();

    init.join();

    consume.join();

    return 0;
}
#包括“MainThread.h”
#包括
#包括
#包括
#包括
#包括
#包括
std::互斥m;
std::条件变量cv;
bool ready=false;
bool-processed=false;
无效工作线程(无符号int-threadNum)
{
//等待main()发送数据
{
std::唯一锁lk(m);
cv.wait(lk,[{return ready;});
}
标准::cout