简体   繁体   English

VS2010中的多线程

[英]Multithreading in VS2010

I have a C++ DLL created in VS2010. 我在VS2010中创建了一个C ++ DLL。 This DLL is responsible for generating a report and sending it to a server. 该DLL负责生成报告并将其发送到服务器。 This DLL is called by an another application which actually creates the data. 实际创建数据的另一个应用程序调用此DLL。 Since sending the file to a server takes some time so, I don't want the calling application to wait till the DLL is busy in sending the file to a server. 由于将文件发送到服务器需要一些时间,因此,我不希望调用应用程序等待DLL忙于将文件发送到服务器。

To deal with the above-mentioned issue, I want to assign the task of sending file to a server (just one small function) to a separate thread and do not want to wait for this thread to complete (otherwise no benefit). 为了解决上述问题,我想将发送文件到服务器的任务(只是一个小功能)分配给单独的线程,并且不想等待此线程完成(否则没有好处)。

while(UserShutDownTheApp)
{
    - Main App Gather Data 
    - Main App Call data management DLL and pass data to it
        - DataManagementDLL: Generate a Report
        - DataManagementDLL: Start a THREAD and Send report to Server by this THREAD. Do not wait for this THREAD to complete
    - Main App finishes this iteration and is going to enter into next iteration.    
}

Since, I do not have control on the main calling application, I can make modifications only inside the DLL. 由于我对主调用应用程序没有控制权,因此只能在DLL内进行修改。 I am confused how should I implement this logic? 我很困惑该如何实现这种逻辑? I mean, what happens if the Thread has not completed the task of sending the file to the server and meanwhile the main calling application has already called this DLL the second time. 我的意思是,如果线程尚未完成将文件发送到服务器的任务,而主调用应用程序已经第二次调用此DLL,会发生什么情况。

You should add a bit more logic to your DLL. 您应该向DLL添加更多逻辑。 First of all, the call to the DLL should not directly sends the data to the server, but queue the task (simply add all passed data to internal queue). 首先,对DLL的调用不应直接将数据发送到服务器,而应将任务排队(只需将所有传递的数据添加到内部队列中)。 The DLL can have internal worker thread which should be responsible for processing the queue of tasks. DLL可以具有内部工作线程,该工作线程应负责处理任务队列。

How internal implementation and architecture should look like, depends on existing API (is it modifiable or not). 内部实现和架构的外观取决于现有的API(是否可修改)。

Hint - please take a look at DllMain() https://msdn.microsoft.com/en-us/library/windows/desktop/ms682583(v=vs.85).aspx - it should be helpful as an entry point. 提示-请查看DllMain() https://msdn.microsoft.com/zh-cn/library/windows/desktop/ms682583 ( v= vs.85).aspx-作为入口点应该有所帮助。

it is not clear all your constraint but this is a simple example to use threads in c++: 尚不清楚所有约束,但这是在c ++中使用线程的简单示例:

void sendreport(std::string data)
{
    std::cout << "send " << data;
    std::this_thread::sleep_for(std::chrono::seconds(10));

}

std::queue<std::thread> jobs;

void addjob(std::string data)
{
    while (jobs.size() > 10)
    {
        jobs.front().join();
        jobs.pop();
    }

    jobs.emplace(sendreport, data);
}

void waitalljobs()
{
    while (jobs.size() > 0)
    {
        jobs.front().join();
        jobs.pop();
    }
}


int main()
{

    addjob("first");
    addjob("second");
    waitalljobs();
}

In this sample I use a queue and I limit the number of current thread to 10. At the termination of the program waitalljobs wait for all thread had finished their works. 在此示例中,我使用队列,并将当前线程的数量限制为10。在程序终止时,waitalljobs等待所有线程完成其工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM