本文介绍了如何让C ++ 11线程运行几个不同的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C ++ 11中的新的多线程技术。几乎所有我在网上阅读的教程教会如何启动一个新的线程(或几个线程)执行一个函数,如何加入(或分离)线程(或线程)以后,如何避免赛车条件使用 mutex 等。

I am learning the new multi-threading techniques in C++11. Almost all the tutorials I read on the web is teaching how to launch a new thread(or several threads) executing a function, how to join (or detach) the thread(or threads) later and how to avoid racing conditions using mutex, etc.

但我没有看到任何显示如何让线程在不同的部分执行几个函数的程序。问题是,用C ++ 11线程,是可能实现以下?如果是,如何? (例子很好)。

But I don't see any of them showing how to make a thread execute several functions at different parts of the program. The question is, with C++11 threads, is it possible to achieve the following? If so, how? (Giving an example will be great).

void func1(std::vector<int> & data1){ ... }

void func2(std::vector<int> & data2){ ... }

//  main function version I
int main(){
   std::vector<int> data1; 
   // prepare data1 for func1;
   std::thread t1(func1, std::ref(data1));

   std::vector<int> data2; 
   // prepare data2 for func2;
   if (func1 in t1 is done){ 
         t1(func2, std::ref(data2)); 
   }

   t1.join();
   return 0;      
}

此外,如果我想把上面的 main 将函数体放入循环中,如下所示。是否可以?如果是,如何?

And further, what if I want to put the the above main function body into a loop, as following. Is it possible? If so, how?

//main function version II
int main(){
   std::vector<int> bigdata1;
   std::vector<int> bigdata2;

   std::thread t1; // Can I do this without telling t1 the function 
                   // to be executed?

   for(int i=0; i<10; ++i){
       // main thread prepare small chunk smalldata1 from bigdata1 for func1;

       if(t1 is ready to execute a function){t1(func1, std::ref(smalldata1));}

       // main thread do other stuff, and prepare small chunk smalldata2 from bigdata2 for func2;

       if (func1 in t1 is done){ 
            t1(func2, std::ref(smalldata2)); 
       }
   }

   t1.join();
   return 0;      
}


推荐答案

http://www.cplusplus.com/reference/thread/thread/thread/ =nofollow> cplusplus.com

Reference from cplusplus.com:

因此 std :: thread t 根本不定义可执行线程。创建线程时必须提供线程函数,并且之后不能设置。

Therefore std::thread t simply doesn't define an executable thread. A thread function has to be provided when creating the thread and it cannot be set afterwards.

对于您的主要功能版本I,您必须创建两个主题。如下所示:

For your main function version I, you will have to create two threads. Something as the following:

int main(){
    std::vector<int> data1; 
    // prepare data1 for func1;
    std::thread t1(func1, std::ref(data1));

    std::vector<int> data2; 
    // prepare data2 for func2;
    t1.join(); // this is how you wait till func1 is done

    // you will have to create a new thread here for func2
    std::thread t2(func2, std::ref(data2)); 
    t2.join(); // wait for thread2 (func2) to end

    return 0;      
}

同样,你可以把它们放在一个循环中,您主要功能版本II

Similarly, you can put them in a loop and it is alright which will give you main function version II.

这篇关于如何让C ++ 11线程运行几个不同的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 10:18