简体   繁体   English

无效operator()()的理解

[英]void operator()() understanding

I am learning how to use thread in boost library, during my search I found the code below: 我正在学习如何在Boost库中使用线程,在搜索过程中,我发现了以下代码:

struct count
{
    count(int id) : id(id) {}

    void operator()()
    {
         for (int i = 0; i < 10; ++i)
         {
             boost::mutex::scoped_lock
             lock(io_mutex);
             std::cout << id << ": " << i << std::endl;
         }
    }
    int id;
};

int main(int argc, char* argv[])
{
    boost::thread thrd1(count(1));
    boost::thread thrd2(count(2));

    thrd1.join();
    thrd2.join();
    return 0;
}

This program works fine, but my question is, what is the behaviour of the void operator()() function? 该程序运行良好,但是我的问题是, void operator()()函数的行为是什么? Because there are two threads and each one initializes the count with different values. 因为有两个线程,每个线程用不同的值初始化计数。 It's two main count variables are created seperately, so their void operator()() should be different for each variable. 这是两个主要的count变量,它们是分别创建的,因此对于每个变量,它们的void operator()()应该不同。 But it seems that void operator()() is the same for both threads. 但是似乎void operator()()对于两个线程都是相同的。 One more thing in this code void operator()() is not being called from anywhere, so how it is running? 此代码中的另一件事是void operator()()不会从任何地方调用,因此它如何运行?

Can someone please explain me what is going on inside this function? 有人可以解释一下此功能内部发生的情况吗?

void operator()() 

This is a tricky syntax here. 这是一个棘手的语法。

boost::thread thrd1(count(1));
boost::thread thrd2(count(2));

In those two lines, the operator() is NOT called directly. 在这两行中, 不是直接调用operator() The count(n) visible here is the constructor . 这里可见的count(n)构造函数 Then, the objects constructed with '1' and '2' values are passed to a thread objects, which then internally calls operator() to run the specific task on that different thread created for this purpose. 然后,将使用“ 1”和“ 2”值构造的对象传递给thread对象,然后该thread对象在内部调用operator()以在为此目的而创建的不同线程上运行特定任务。

But since two objects remembered different values (1/2) in their ctors, the common code in operator() operates on different actual values of id field. 但是由于两个对象在其ctor中记住了不同的值(1/2),所以operator()的通用代码对id字段的不同实际值进行操作。 That's why the operator has no parameters: all required params are stored in the 'count' object during its construction. 这就是为什么运算符没有参数的原因:所有必需的参数在构造期间都存储在“计数”对象中。

Btw. 顺便说一句。 to construct an object and call the operator() , a line would look like this: 构造一个对象并调用operator() ,一行如下所示:

count(1)();

The syntax of operator overloading in C++ is : C ++中运算符重载的语法为:

return-type operator operator-name (args); 返回类型的运算符运算符名称(args); Example : int operator+(int other) Here the operator name is "+". 示例:int operator +(int other)此处的操作符名称为“ +”。 In your case, it's "()" 您的情况是“()”

Now here, you've got 2 instances of thread, each one having it's own instance of count. 现在,您有2个线程实例,每个实例都有自己的count实例。 When the thread calls operator()(), it reads the count value of the particular instance of thread which is why you have different results for the same operator 当线程调用operator()()时,它将读取线程的特定实例的计数值,这就是为什么您对同一运算符有不同结果的原因

It would certainly help fi you read the context of the code (and post a link here for other readers :)). 当然,这将有助于您阅读代码的上下文(并在此处为其他读者提供链接:)。

Without any other context to help me, I'd guess this was a demonstration for new learners of boost::thread. 没有任何其他背景可以帮助我,我想这是对boost :: thread新手的演示。 I'll assume you know how threads work - this link is a good beginners concepts reference if you're new. 我假设您知道线程如何工作-如果您是新手, 此链接是一个不错的初学者概念参考。

To break it down: 分解:

    boost::thread thrd1(count(1));
    boost::thread thrd2(count(2));

create two threads with different Ids. 创建两个具有不同ID的线程。 As @quetzalcoatl mentions, the boost thread takes in a function object that defines the operator ()() and executes the contents of that operator in a different thread. 正如@quetzalcoatl所提到的,boost线程接受一个函数对象,该对象定义了operator ()()并在另一个线程中执行该操作符的内容。 This means we have 3 threads now - the main execution thread and the 2 threads that we've created above. 这意味着我们现在有3个线程-主执行线程和上面创建的2个线程。

    thrd1.join();
    thrd2.join();

the main execution thread waits for the other two threads to join - ie finish their processing and indicate to the main thread that they are done. 主执行线程等待其他两个线程加入 -即完成它们的处理并向主线程指示它们已完成。 When they join the thread is terminated (note that thrd1 is a boost thread object - used to reference the actual thread we create behind the scenes, and will be available till the stack ends). 当他们加入线程时,线程终止(请注意thrd1是boost线程对象-用于引用我们在后台创建的实际线程,并且在堆栈结束之前可用)。

The operator()() is a way to monitor a. operator()()是监视a的方法。 which thread is running and b. 哪个线程正在运行,以及b。 how many ' skipping-between-threads ' happen. 发生了多少个“ 线程间跳过 ”。 Before explaining, let's see the result. 在解释之前,让我们看看结果。 It's likely to be something like (but probably not exactly): 它可能类似于(但可能不完全一样):

1: 0
1: 1
2: 0
2: 1
2: 2
2: 3
1: 2 ...    

So the program (in this case) runs two loops of thread #1, switches to thread #2, runs a few loops, comes back to #1 and continues running. 因此,程序(在这种情况下)运行线程#1的两个循环,切换到线程#2,运行几个循环,返回到#1,然后继续运行。 There is a reason both threads can't go into the loop at the same time - the boost scoped_lock grabs hold of the (external) io_mutex, locks it down for that thread's use and proceeds to run till the scoped_lock object is destroyed and the io_mutex object released. 这是两个线程无法同时进入循环的原因 -boost scoped_lock抓住了(外部)io_mutex,将其锁定为该线程的使用并继续运行,直到scoped_lock对象被销毁并且io_mutex对象已释放。 This means, while both threads are running parallelly, only one object has access to that specific stack after the "scoped_lock" call. 这意味着,当两个线程并行运行时,在“ scoped_lock”调用之后,只有一个对象可以访问该特定堆栈。

Hope that helps. 希望能有所帮助。

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

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