C++11 this_thread::yeild(12)

原文地址:http://www.cplusplus.com/reference/thread/this_thread/yield/

function
<thread>

std::this_thread::yield

void yield() noexcept;
Yield to other threads
The calling thread yields, offering the implementation the opportunity to reschedule.
挂起当前线程的运行,给其他线程运行的机会。

This function shall be called when a thread waits for other threads to advance without blocking.

该方法应该在一个线程等待其他线程资源的时候不阻塞地调用。(即等待其他资源的时候,应该挂起该线程)


Parameters

none

Return value

none

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
26
// this_thread::yield example
#include <iostream>       // std::cout
#include <thread>         // std::thread, std::this_thread::yield
#include <atomic>         // std::atomic

std::atomic<bool> 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;
}


Possible output (last line may vary):
race of 10 threads that count to 1 million...
6189370542

Exception safety

No-throw guarantee: never throws exceptions.


—————————————————————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:coderguang@gmail.com

2014-9-4

于GDUT

——————————————————————————————————————————————————————————————————






yield是Python的关键字,用于定义生成器函数。生成器函数与普通函数不同,它可以被中断和恢复,并且在每次中断时可以产生一个值。生成器函数通过使用yield语句来生成值,当函数执行到yield语句时,会暂停并返回yield后面的值,下次调用生成器函数时会从上次暂停的地方继续执行。生成器函数通常用于处理大量数据的情况下,可以节省内存空间和提高性能。 例如,如果我们定义一个生成器函数evenNumber,它可以生成一系列偶数: def evenNumber(max): n = 0 while n < max: yield n n += 2 通过调用evenNumber函数并使用for循环来迭代生成器的值,我们可以依次输出偶数序列。 在生成器函数中,yield语句的作用是将n的值返回给调用者,并且暂停函数的执行。当再次调用生成器函数时,函数会从上次暂停的地方继续执行,直到遇到下一个yield语句或函数结束。这也是为什么在第一次调用evenNumber函数时,打印语句"第二次是从这里开始的"并没有执行。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Python关键字yield](https://blog.csdn.net/weixin_44726976/article/details/109058763)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [如何理解python中的yield,看完你还是不懂,我手把手教你](https://blog.csdn.net/bo_mask/article/details/124738180)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值