Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
98 views
in Technique[技术] by (71.8m points)

c++ - Calling a function which is inside a Thread

I was doing Unit Testing and finding code coverage using gtest and lcov. My function is

void MyClass::MyFunction(const std::string& argument1, const std::string& argument2) {
    std::thread([this, argument1, argument2]() {
        std::unique_lock<std::mutex> live_lock_(live_mutex_);
        int64_t time_stamp = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
        int64_t time_to_live = myList[argument1] - (time_stamp % myList[argument1]); 
        cond_var_.wait_for(time_to_live_lock_, std::chrono::milliseconds(time_to_live), [this, argument1] { return cond_var_status_[argument1]; });
        if (message_master_.find(argument1) != message_master_.end()) {
            //something
        }
    }).detach();
    std::cout<< "end line is executed"<<std::endl; }

and my test function is

TEST(test, test1) {
    Myclass testObj;
    testObj.MyFunction("arg1", "arg2");
    ASSERT_EQ("","");
};

When I run the test, all codes except those inside thread are executed. So is there any solution to call those codes inside thread too?

question from:https://stackoverflow.com/questions/65881112/calling-a-function-which-is-inside-a-thread

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How do you know none of those lines are being executed? I would suggest making a test program, compiling with -g and -Wall, then using gdb to make sure a thread is actually being created. If it is, step through the code. It could be that the compiler gets rid of code it thinks is doing nothing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...