Thread invalid use of ‘this’ in non-member function
Feb 6, 2019 at 5:50am UTC
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...
Feb 6, 2019 at 6:29am UTC
> 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() ;
}
Feb 6, 2019 at 7:43am UTC
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?
Feb 6, 2019 at 8:04am UTC
Last edited on Feb 6, 2019 at 8:04am UTC
Feb 6, 2019 at 8:14am UTC
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
Feb 6, 2019 at 8:15am UTC
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.
Feb 6, 2019 at 8:24am UTC
Add -pthread to the link options too: LDFLAGS = -pthread
Topic archived. No new replies allowed.