C ++ Timer暂停程序执行直到完成 [英] C++ Timer pausing execution of program until finished

查看:383
本文介绍了C ++ Timer暂停程序执行直到完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++,XCode 4.6.3,OSX 10.8.2,在iOS上部署



我试图创建一个定时事件。



我的思想过程是创建一个线程,在它的时间,然后在最后有它调用另一个函数。这是工作,但它是暂停的程序的其余部分。

  //启动线程
std :: thread t1(start_thread);

//加入主线程的线程
t1.join();

void start_thread()
{
std :: cout< 线程开始< std :: endl;

auto start = std :: chrono :: high_resolution_clock :: now();

std :: this_thread :: sleep_until(start + std :: chrono :: seconds(20));

stop_thread();
}

void stop_thread()
{
std :: cout< 线程停止了。 << std :: endl;
}

有没有办法不暂停程序执行? / p>

更新:



我可以在头文件中声明线程并加入stop_thread p>

  void stop_thread()
{
std :: cout< 线程停止。 << std :: endl;
ti.join();
}

但是会引发:



类型'std :: thread'不提供调用操作符



更新2:调用t1.detach >
下面是一个来自cpp参考的例子

http://en.cppreference.com/w/cpp/thread/thread/detachrel =nofollow> http://en.cppreference.com/w/cpp/thread/thread/detach

  #include< iostream> 
#include< chrono>
#include< thread>

void independentThread()
{
std :: cout< Starting concurrent thread.\\\
;
std :: this_thread :: sleep_for(std :: chrono :: seconds(2));
std :: cout<< 退出并发thread.\\\
;
}

void threadCaller()
{
std :: cout< Starting thread caller.\\\
;
std :: thread t(independentThread);
t.detach();
std :: this_thread :: sleep_for(std :: chrono :: seconds(1));
std :: cout<< 退出线程caller.\\\
;
}

int main()
{
threadCaller();
std :: this_thread :: sleep_for(std :: chrono :: seconds(5));
}

输出:
开始线程调用。
启动并发线程。
退出线程调用者。
退出并发线程。



我们看到并发线程在线程调用者结束后结束。如果不调用分离,这是不可能的。



希望有帮助,但Jason找到了解决方案。


C++, XCode 4.6.3, OSX 10.8.2, deploying on iOS

I am trying to create a timed event.

My thought process was to create a thread, do the timing in it and then at the end have it call another function. This is working however it is pausing the rest of the program.

//Launch a thread
std::thread t1(start_thread);

//Join the thread with the main thread
t1.join();

void start_thread()
{
    std::cout << "thread started" << std::endl;

    auto start = std::chrono::high_resolution_clock::now();

    std::this_thread::sleep_until(start + std::chrono::seconds(20));

    stop_thread();
}

void stop_thread()
{
    std::cout << "thread stopped." << std::endl;
}

Is there a way to do this that doesn't pause program execution?

Update:

I could declare the thread in the header file and join in the stop_thread():

void stop_thread()
{
    std::cout << "thread stopped." << std::endl;
    ti.join();
}

but that throws:

Type 'std::thread' does not provide a call operator

UPDATE 2: Calling t1.detach() instead of join seems to work.

解决方案

You're right it works: Here is an example coming from cpp reference http://en.cppreference.com/w/cpp/thread/thread/detach

#include <iostream>
#include <chrono>
#include <thread>

void independentThread() 
{
    std::cout << "Starting concurrent thread.\n";
    std::this_thread::sleep_for(std::chrono::seconds(2));
    std::cout << "Exiting concurrent thread.\n";
}

void threadCaller() 
{
    std::cout << "Starting thread caller.\n";
    std::thread t(independentThread);
    t.detach();
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::cout << "Exiting thread caller.\n";
}

int main() 
{
    threadCaller();
    std::this_thread::sleep_for(std::chrono::seconds(5));
}

Output: Starting thread caller. Starting concurrent thread. Exiting thread caller. Exiting concurrent thread.

We see that the concurrent thread ends after the thread caller ends. This is not possible if detach is not called.

Hope that helps, but Jason found the solution.

这篇关于C ++ Timer暂停程序执行直到完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆