boost c ++中的多线程 [英] Multithreading in boost c++

查看:102
本文介绍了boost c ++中的多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手来提升线程,我有一个循环的char类型。我的for循环我调用的函数根据输入字符进行不同的操作....下面是我的代码



Hi,I am new to boost threads and i have a char type for loop.Inside my for loop i am calling a function which does different operations based on the input char....below is my code

char * letters = "ABCDEFGHIJKLMNOPQRSTUVWXZ";

  for (char * ptr = letters; *ptr != 0; ++ptr)
  {
    char ch = *ptr;
    str<< ch;
    ScaleData(str,Vector_node_double,ArrayIndex);
    ArrayIndex++;
    str.str("");
    str.clear();

  }





在ScaleData()函数内部我计算一些值并将它们存储到一个数组中.size数组的数量是26(它取决于for循环的迭代)..完成for循环后我得到了数组中最大的值。我的问题是我想在不同的线程中同时运行循环并将每个计算值存储在数组中..



ex:



Inside ScaleData() function i compute some values and store them into an array..size of the array is 26 (it depends on the iterations of the for loop)..After finishing the for loop i got the biggest value from the array..My question is i want to to run for loop in different threads at the same time and store each computed value in the array..

ex :

ScaleData(A,Vector_node_double,ArrayIndex); // thread one
ScaleData(B,Vector_node_double,ArrayIndex); // thread two
ScaleData(C,Vector_node_double,ArrayIndex); // thread three
ScaleData(D,Vector_node_double,ArrayIndex); // thread four
.
.
.





需要帮助的人......: - )



Need help guys... :-)

推荐答案

使用障碍 [< a href =http://www.boost.org/doc/libs/1_54_0/doc/html/thread/synchronization.html#thread.synchronization.barrierstarget =_ blanktitle =New Window> ^ ] - '技巧'是所有线程,包括在其他线程完成时应该执行某些操作的线程,必须通过屏障。



最好的问候

Espen Harlinn
Use a barrier[^] - the 'trick' is that all the threads, including the thread that is supposed to do something when the other threads have finished, have to 'pass' the barrier.

Best regards
Espen Harlinn


阅读本文档中关于提升线程的部分:



http://www10.informatik.uni-erlangen.de/教学/课程/ SS2011 / CPP / czilli.pdf [ ^ ]





这里有一个例子:



http://stackoverflow.com/questions/11407577 /如何使用 - 提升障碍 [ ^ ]
Read the section on boost threads in this document:

http://www10.informatik.uni-erlangen.de/Teaching/Courses/SS2011/CPP/czilli.pdf[^]


An example here:

http://stackoverflow.com/questions/11407577/how-to-use-boost-barrier[^]


这篇关于boost c ++中的多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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