std::this_thread

Interface get_id

Interface yield

Interface sleep_until

Interface sleep_for


std::this_thread里有 访问 当前线程的 接口函数.

Interface get_id

函数原型: thread::id get_id() noexpect;   // 返回 calling thread的 thread id

thread::id 是 thread的唯一标识

#include        // std::cout
#include          // std::thread, std::thread::id, std::this_thread::get_id
#include          // std::chrono::seconds
 
std::thread::id main_thread_id = std::this_thread::get_id();

void is_main_thread() {
  if ( main_thread_id == std::this_thread::get_id() )
    std::cout << "This is the main thread.\n";
  else
    std::cout << "This is not the main thread.\n";
}

int main() 
{
  is_main_thread();
  std::thread th (is_main_thread);
  th.join();
}

Interface yield

函数原型: void yield() noexpect;

yield() 使得 calling thread 交出 时间片, 使calling thread 占用CPU时间大大减少.

例: 某个线程需要等待 某个操作完成

while (!isDone());  // 用一个循环不断判断isDone()操作是否完成, 使得该线程占满CPU时间, 造成资源浪费

while (!isDone()) std::this_thread::yield(); // 判断一次操作isDone()是否完成; 如果没有完成, 调用yield()函数交出时间片, 过一会儿再来判断是否完成, so该线程占用CPU时间会大大减少

#include        // std::cout
#include          // std::thread, std::this_thread::yield
#include          // std::atomic

std::atomic ready (false);

void count1m(int id) {
  while (!ready) {             // wait until main() sets ready...
    std::this_thread::yield();
  }
  for (volatile int i=0; i<1000000; ++i) {}
  std::cout << id;
}

int main ()
{
  std::thread threads[10];
  std::cout << "race of 10 threads that count to 1 million:\n";
  for (int i=0; i<10; ++i) threads[i]=std::thread(count1m,i);
  ready = true;               // go!
  for (auto& th : threads) th.join();
  std::cout << '\n';

  return 0;
}

Interface sleep_until

函数原型: template

                    void sleep_until (const chrono::time_point& abs_time);

Block calling thread 直到 abs_time.(abs_time为某年某月某日某时某分某秒). Stop current thread 的执行, 直到过了abs_time; 以便other threads继续执行.

#include        // std::cout
#include         // std::put_time
#include          // std::this_thread::sleep_until
#include          // std::chrono::system_clock
#include           // std::time_t, std::tm, std::localtime, std::mktime

int main() 
{
  using std::chrono::system_clock;
  std::time_t tt = system_clock::to_time_t (system_clock::now());

  struct std::tm * ptm = std::localtime(&tt);
  std::cout << "Current time: " << std::put_time(ptm,"%X") << '\n';

  std::cout << "Waiting for the next minute to begin...\n";
  ++ptm->tm_min; ptm->tm_sec=0;
  std::this_thread::sleep_until (system_clock::from_time_t (mktime(ptm)));

  std::cout << std::put_time(ptm,"%X") << " reached!\n";

  return 0;
}

std::this_thread_第1张图片

Interface sleep_for

函数原型为: template

                    void sleep_for (const chrono::duration& rel_time);

Block calling thread rel_time时间. Stop current thread 执行 rel_time时间.

#include        // std::cout, std::endl
#include          // std::this_thread::sleep_for
#include          // std::chrono::seconds
 
int main() 
{
  std::cout << "countdown:\n";
  for (int i=10; i>0; --i) {
    std::cout << i << std::endl;
    std::this_thread::sleep_for (std::chrono::seconds(1));
  }
  std::cout << "Lift off!\n";

  return 0;
}

 

你可能感兴趣的:(std::this_thread)