简体   繁体   English

线程完成后在另一个线程中调用方法

[英]Calling a method in another thread after a thread is finished

I'm trying to parallelize my program, but since I'm very new to threads, I'm facing some problems. 我正在尝试并行化程序,但是由于我对线程还很陌生,所以我遇到了一些问题。

I have two methods which are part of the same class. 我有两个属于同一类的方法。 One of the methods does some calculations in a for loop and pushes the results in a vector, other method (runTheResult) takes the vector and launches a thread using the obtained vector. 一种方法在for循环中执行一些计算,然后将结果压入向量,另一种方法(runTheResult)接受向量,并使用获得的向量启动线程。 I wish to launch another thread for running the next obtained result every time runTheResult is done with a result while limiting the maximum number of threads at a time to 4. 我希望每次运行runTheResult时都会启动另一个线程以运行下一个获得的结果,同时将一次将最大线程数限制为4。

The structure of my program is like: 我的程序的结构如下:

void runTheResult(vector<double>& u){

//process 'u' and launch a thread 


};

void method(){

for(...){

//calculate

    for(...){

    //put the calculations in vector<double>result

    };

    runTheResult(result); 

};

};

I've googled about this a lot and one of the solutions is to maintain a message que. 我已经对此进行了很多搜索,其中一种解决方案是维护消息队列。 The problem with this, however, is if I implement a que, I'll have to check the que with another thread periodically in a while loop. 但是,与此有关的问题是,如果我实现一个查询,则必须在while循环中定期与另一个线程检查该查询。 If I use while loop like while(true){//check for new messages if number of threads is less than five} , I'll lose a lot of processing power and if I choose to put the loop to sleep if condition is not met, I waste processing power. 如果我像while(true){//check for new messages if number of threads is less than five}那样使用while循环while(true){//check for new messages if number of threads is less than five}while(true){//check for new messages if number of threads is less than five} ,我将失去很多处理能力,如果在条件不满足的情况下选择让循环进入休眠状态遇见,我浪费了处理能力。 The functions I'm running in threads take 2-5 seconds each and I've to process ~1k to 50k of them so even a second of delay per loop is a lot. 我在线程中运行的函数每个需要2-5秒,而我必须处理其中的大约1k至50k,因此每个循环甚至一秒钟的延迟都很大。

Is it possible to run runTheResultin another thread every time runTheResult is done? 每次runTheResult完成时,是否可以在另一个线程中运行runTheResult? or is there better way to do this? 还是有更好的方法做到这一点?

Others are telling you to use message queue because that's the safest way to do it. 其他人则告诉您使用消息队列,因为这是最安全的方法。 Your program must have at least a main thread that the user(you or end-user) can interact with. 您的程序必须至少具有一个用户(您或最终用户)可以与之交互的主线程。 This main thread will be looping for as long as your program runs. 只要您的程序运行,该主线程就会一直循环。 You do your message processing here 您在这里进行邮件处理

// this is not actually running the result now
// this only sends it to the main thread that will run the result
void runTheResult(vector<double>& u){ 

    //process 'u' and launch a thread. 
    // @NOTE Launching a thread again will not be beneficial as it will still be blocked 
    // by the mutex

    // convert/store vector into Message. To make it usable for other types
    // or you can just change Message to double
    Message u_message = to_message(u)

    std::lock_guard<std::mutex> lock(message_mutex);
    messages_shared.append(u_message);

};

void method() // runs on worker thread
{
    for(...){

    //put the calculations in vector<double>result

    };

    runTheResult(result);
}

void getMessages_safe(std::vector<Messages>& outMessages_safe)
{
    // as Ted Lyngo suggests, using lock_guard is best practice. See edit for alternative
    std::lock_guard<std::mutex> lock(message_mutex);
    outMessages_safe = messages_shared;
    messages_shared.clear();
}

std::vector<Message> messages_shared;
std::mutex message_mutex;

void main() { // this runs on the very first thread of the program
  while (isProgramRunning)
  {
      std::vector<Message> messages_safe; // safe to access by this thread only
      getMessages_safe(messages_safe);

      // dispatch messages to whoever needs it

      // launch worker thread
  }
}

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

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