简体   繁体   English

有条件的c ++ 11线程

[英]conditional c++11 threading

I'm trying to understand threading in c++11. 我正在尝试了解c ++ 11中的线程。 Suppose I have an array arr={5,5,5,5,5}, and I want to calculate the value of {5^a^b, 5^a^b, 5^a^b, 5^a^b, 5^a^b} using two threads, with parameter a or b passed to the threads. 假设我有一个数组arr = {5,5,5,5,5},并且我想计算{5 ^ a ^ b,5 ^ a ^ b,5 ^ a ^ b,5 ^ a ^的值b,5 ^ a ^ b}使用两个线程,并将参数a或b传递给线程。 How do I guarantee that the seconds thread acts on each term only after the first thread has worked on it (the difference between 5^a^b and 5^b^a)? 我如何保证秒线程仅在第一个线程处理完之后才对每个术语起作用(5 ^ a ^ b和5 ^ b ^ a之间的差)?

Thanks. 谢谢。

You may share an atomic variable between threads, that would represent the most-left index in array - of element that is not yet processed by first thread. 您可以在线程之间共享一个原子变量,该原子变量表示尚未由第一个线程处理的元素的数组中最左边的索引。

Increment index in the first thread after operation with a and loop in the second thread until index is less than size of an array. 在第二个线程中a and循环操作之后,在第一个线程中递增索引,直到索引小于数组的大小为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM