0

I understand from my previous query that it migth not be possible to create a std::thread without initialization.

BUt WRT below code snippet, I see I'm not even able to create a std::thread even with initialization as a data member of a class/struct in C++.

It complains with compilation error "Function threadfunction is not a type name", irrespective if the function is within class/struct or outside (global). Please refer below example. Seems like there is some rivalry between class/struct and std::threads. Seeing so much of issues.

int f1234()
{
std::cout<""f1234";
}
struct Abc
{
    int a;
    void mem1()
    {
        printf("mem1\n");
    }
    std::thread mt(f1234); // Function f1234 is not a type name
    std::thread mt1(mem1); // Function mem1 is not a type name

}
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
codeLover
  • 3,720
  • 10
  • 65
  • 121
  • 1
    It has nothing to do with threads. Try a string or a vector or an int and see. – juanchopanza Jul 11 '17 at 06:06
  • Also, you misunderstood. You can default initialize a thread. – juanchopanza Jul 11 '17 at 06:07
  • 2
    To do inline-initialization you either have to use assignment-syntax or curly-braces. **But** that's not the only problem you have: The function `Abc::mem1` is a *member* function. Those need to be called on an *object* which you don't have when doing inline-initialization. – Some programmer dude Jul 11 '17 at 06:10
  • By the way, you should probably read about [constructor member initializer lists](http://en.cppreference.com/w/cpp/language/initializer_list). – Some programmer dude Jul 11 '17 at 06:16
  • Does this correct this issue. I have chnaged the way std::thread is initialized ? std::thread mt = std::thread(f1234); // Function f1234 is not a type name std::thread mt1 = std::thread(&Abc::mem1); // Function mem1 is not a type name – codeLover Jul 11 '17 at 06:40

0 Answers0