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

嗯嗯嗯

我正在尝试并行化我的程序,但由于我对线程非常陌生,因此遇到了一些问题。

我有两个属于同一类的方法。其中一种方法在 for 循环中进行一些计算并将结果推送到向量中,另一种方法 (runTheResult) 获取向量并使用获得的向量启动线程。我希望在每次 runTheResult 完成结果时启动另一个线程来运行下一个获得的结果,同时将一次的最大线程数限制为 4。

我的程序的结构是这样的:

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); 

};

};

我在谷歌上搜索了很多,解决方案之一是维护消息队列。然而,这样做的问题是,如果我实现了一个 que,我将不得不在 while 循环中定期用另一个线程检查 que。如果我使用 while 循环while(true){//check for new messages if number of threads is less than five},我会失去很多处理能力,如果我选择在不满足条件的情况下让循环进入睡眠状态,我会浪费处理能力。我在线程中运行的函数每个需要 2-5 秒,我必须处理大约 1k 到 50k,所以即使每个循环一秒的延迟也很多。

每次 runTheResult 完成时,是否可以在另一个线程中运行 runTheResult?或者有更好的方法来做到这一点?

她的十字架的以色列

其他人告诉您使用消息队列,因为这是最安全的方法。您的程序必须至少有一个用户(您或最终用户)可以与之交互的主线程。只要您的程序运行,这个主线程就会一直循环。你在这里做你的消息处理

// 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
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 ThreadStart 线程完成后运行另一个函数

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

如何在另一个循环中循环线程句柄并在完成后加入?

如何在另一个方法完成后启动一个方法

如何从另一个线程调用控制方法

如何在主线程中调用一个方法并终止另一个线程?

如何告诉另一个线程一个线程在recv()调用中* now *

另一个完成后执行循环

从另一个文件调用函数并在完成后获取结果?

Java Struts在执行动作完成后调用另一个动作

Ajax完成后调用另一个Ajax函数

在Kotlin中,处理程序线程会等待另一个线程完成吗?

我正在尝试从线程1调用线程2(在一个循环中调用五次),但是线程2仅在线程1中的循环完成后才开始执行

从线程内部调用方法到python pyqt中的另一个类

NSURLConnection在另一个线程中启动。委托方法未调用

如何在另一个线程中调用方法?

当另一个使用多线程完成时,启动一个方法

等到函数完成后再继续执行主线程(一个线程)

在另一个完成后使另一个可观察

Angular / TypeScript-在另一个函数完成后调用一个函数

TypeScript / Angular 2-另一个完成后调用一个函数

如何从另一个线程调用线程的getHandler()方法?

从另一个线程调用方法时,主线程被阻塞

线程安全地从另一个线程中调用TimerTask

从另一个线程调用主线程中的函数?

如何通知一个线程,另一个线程已完成执行?

Python - 在上一个线程完成后运行下一个线程

Python多线程使用另一个线程中的一个线程的数据

AngularJS在另一个函数完成后执行一个函数