c++ 11, multi thread questions

Thread Starter

bug13

Joined Feb 13, 2012
2,002
Hi team

Just playing around with C++11's multi-threading stuff, the follow code puzzle me, why my test1 below need double ()??

Here is the code:
C++:
/* -----------------------------------------
* MinGW-W64, v8.1.0
* Win10, 64-bit system
*------------------------------------------ */

#include <iostream>
#include <vector>
#include <string>
#include <thread>

using namespace std;

class test1
{
    public:
        void    operator()()    
        {
            for(int i = 0; i < 10; i++)
                cout << "TEST1-" << i << endl;
        }
};

class test2
{
    public:
                test2(int x):_x(x){}
        void    operator()()    
        {
            for(int i = 0; i < _x; i++)
                cout << "TEST2-" << i << endl;
        }
    private:
        int _x;
};


int main()
{

    std::thread threadObj1((test1())); // why double () here??, doesn't work without the double () eg: std::thread threadObj1(test1())
    std::thread threadObj2(test2{10});


    threadObj1.join();
    threadObj2.join();

    printf("done\n");
}
 
Last edited:

MrSoftware

Joined Oct 29, 2013
2,202
I've seen similar behavior with some casting, I think it's a compiler order of operations thing. I would need to review the compiler specs to say if it's a bug or expected, but I think what's happening is when you just use "test1()" it's evaluating the "test1" part before the "()" part, and the "test1" part is not the correct type. But when you "(test1())" , it evaluates everything inside the outer () first, and the result is the correct type for that use. Maybe someone else can explain it better than I can. @WBahn is usually good with compiler details, maybe he will see this and chime in.
 

xox

Joined Sep 8, 2017
838
Hi team

Just playing around with C++11's multi-threading stuff, the follow code puzzle me, why my test1 below need double ()??

Here is the code:
C++:
/* -----------------------------------------
* MinGW-W64, v8.1.0
* Win10, 64-bit system
*------------------------------------------ */

#include <iostream>
#include <vector>
#include <string>
#include <thread>

using namespace std;

class test1
{
    public:
        void    operator()()   
        {
            for(int i = 0; i < 10; i++)
                cout << "TEST1-" << i << endl;
        }
};

class test2
{
    public:
                test2(int x):_x(x){}
        void    operator()()   
        {
            for(int i = 0; i < _x; i++)
                cout << "TEST2-" << i << endl;
        }
    private:
        int _x;
};


int main()
{

    std::thread threadObj1((test1())); // why double () here??, doesn't work without the double () eg: std::thread threadObj1(test1())
    std::thread threadObj2(test2{10});


    threadObj1.join();
    threadObj2.join();

    printf("done\n");
}

What's happening is the compiler thinks you're declaring a function named "threadObj". You can use the assignment operator, like this:

Code:
std::thread threadObj1 = std::thread(test1());
Better still:

Code:
auto threadObj1 = std::thread(test1());
 

Thread Starter

bug13

Joined Feb 13, 2012
2,002
What's happening is the compiler thinks you're declaring a function named "threadObj". You can use the assignment operator, like this:

Code:
std::thread threadObj1 = std::thread(test1());
Better still:

Code:
auto threadObj1 = std::thread(test1());
Now it makes sense, thanks!
 
Top