Thread invalid use of ‘this’ in non-member function

I'm trying threads for the first time and I've run into an error when attempting to implement in main.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
main {
    tm time1;
    //set up time1;
    std::thread t(&Schedule::timeAlert, this, time1);
    t.join();
}

Schedule.h

class Schedule {
  public:
    void timeAlert(tm time);
}

Schedule.cpp

void Schedule::timeAlert(tm time) {
    sleep_until (system_clock::from_time_t (timegm(&time)));
    cout << "It is " << std::asctime(&time);
}


Why is it treating this function like a non-member? It clearly is a member of Schedule...
> Why is it treating this function like a non-member? It clearly is a member of Schedule

main() is not a member function.
If the function must be non-static, an object of the class type is required.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <thread>
#include <chrono>
#include <ctime>

struct scheduler {

    void wake_up_and_alert_at( std::time_t time ) const {

        std::this_thread::sleep_until( std::chrono::system_clock::from_time_t(time) ) ;
        std::cout << "alert from " << name << ": it is " << std::ctime( std::addressof(time) );
    }

    std::string name ;
};

int main() {

    const auto now = std::time(nullptr) ;
    std::cout << std::ctime( std::addressof(now) );

    scheduler sch { "scheduler X" } ;
    std::thread t( &scheduler::wake_up_and_alert_at, std::addressof(sch), now+5 ) ;
    t.join() ;
}
OK I set it up like this:

1
2
3
Schedule mySchedule;  
std::thread t(&Schedule::timeAlert, std::addressof(mySchedule), time1);
t.join();


And I got this error:

 In function `std::thread::thread<void (Schedule::*)(tm), Schedule*, tm&>(void (Schedule::*&&)(tm), Schedule*&&, tm&)':
/usr/include/c++/7/thread:122: undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status


What does this mean?
It means you didn't pass the compiler flag -pthread that is required to use GCC's implementation of the C++ thread support library.

See the appropriate section of the GCC manual:
https://gcc.gnu.org/onlinedocs/libstdc++/manual/using.html#manual.intro.using.flags
Last edited on
With the GNU tool chain, the compiler option -pthread is required.
For example: g++ -std=c++17 -O3 -Wall -Wextra -pedantic-errors -pthread my_program.cpp
OK I added it in...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
CXX = g++
CXXFLAGS = -c -g -std=c++11 -Wall -W -Werror -pthread -pedantic
LDFLAGS =

schedule: main.o schedule.o
	$(CXX) $(LDFLAGS) main.o schedule.o -o main

main.o : main.cpp
	$(CXX) $(CXXFLAGS) main.cpp

schedule.o: schedule.cpp schedule.h
	$(CXX) $(CXXFLAGS) schedule.cpp

clean :
	rm -f core $(PROG) *.o


But I'm still getting the same error:

In function `std::thread::thread<void (Schedule::*)(tm), Schedule&, tm&>(void (Schedule::*&&)(tm), Schedule&, tm&)':
/usr/include/c++/7/thread:122: undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status


I did a make clean first just to be safe, but that didn't solve it.
Add -pthread to the link options too: LDFLAGS = -pthread
Topic archived. No new replies allowed.