线程c ++防止值发生变化

时间:2011-04-25 14:07:57

标签: c++ multithreading boost mutex

我正在使用boosthread创建3个线程,每次传递不同的参数时调用相同的函数。 例如。 1 / thread.add(function,int a1,std :: string b),thread.add(function,int a2,std :: string b),  thread.add(function,int a3,std :: string b),thread.add(function,int a4,std :: string b)

当线程中的全局值发生更改时,我不希望其他线程执行 并再次更改该值 例如,功能(a,b){

if(该线程发生的事情)value = 5;

//如果什么也没发生 value = 1; }

如果一个线程的值为5,那么我不希望其他线程干扰该值并使其返回1.我该怎么做?感谢。

也许这样做的方法是使用boost:mutex,但我没有看到这样做的任何好处因为这个值只是找到了 在return语句之前,我可能已经使用了boost join_all()。但效率会降低。

1 个答案:

答案 0 :(得分:0)

我有一个例子可以帮助你。 它使用C ++ 11,但可以轻松转换为Boost。

算法很简单,它由两个主要部分组成:

  1. 启动三个主题
  2. 加入他们(等待他们完成)
  3. 每个主题的作用:

    1. 有些工作吗
    2. 如果之前从未初始化变量,则初始化变量(FirstThreadToFinish)
    3. 独特的初始化函数如何工作。  1.使用互斥锁来防止对共享变量的多次访问(保证该函数一次只能由一个线程加入)  2.如果变量尚未初始化,则使用线程名称对其进行初始化,并将布尔值设置为true(变量初始化)  3.否则什么都不做

      #include "stdafx.h"
      #include <thread>
      #include <chrono>
      #include <mutex>
      #include <iostream>
      #include <string>
      using namespace std;
      
      mutex m;//to synchronize the data properly
      string FirstThreadToFinish;
      bool IsInitialized;
      
      //This function is called by the main thread: does the work and initializes the variable only once
      int MyThreadFunction(int Duration, const string & ThreadName);
      
      //this function does some work (put here what your thread is really supposed to do)
      int DoSomeWork(int Duration);
      
      //this function initializes the variable only once and does nothing otherwise
      int InitializeVariableOnce(int Duration, const string & ThreadName);
      
      //this function initializes the variable only once and does nothing otherwise
      int InitializeVariableOnce(int Duration, const string & ThreadName)
      {
          std::lock_guard<mutex> l(m);
          if (!IsInitialized)
          {
              FirstThreadToFinish=ThreadName;
              IsInitialized=true;
              cout<<"FirstThreadToFinish= "<<ThreadName<< ", IsInitialized= "<<IsInitialized<<endl;
          }
          return 0;
      }
      
      //this function does some work (put here what your thread is really supposed to do)
      int DoSomeWork(int Duration)
      {
          std::this_thread::sleep_for(std::chrono::seconds(Duration));
          return 0;
      }
      
      int MyThreadFunction(int Duration, const string & ThreadName)
      {
          DoSomeWork(Duration);
          InitializeVariableOnce(Duration, ThreadName);
          return 0;
      }
      
      int main()
      {
          //at the begining nothing is initialized
          FirstThreadToFinish="Uninitialized";
          IsInitialized=false;
          cout<< "FirstThreadToFinish= "<<FirstThreadToFinish << ", IsInitalized="<<IsInitialized<<endl; 
      
          cout<<"Now launching 3 threads= "<<endl;
          thread MyAThread(MyThreadFunction,1,"AThread");
          thread MyBThread(MyThreadFunction,2,"BThread");
          thread MyCThread(MyThreadFunction,3,"CThread");
      
      
          MyAThread.join();
          MyBThread.join();
          MyCThread.join();
      
          return 0;
      }
      

      希望有帮助,请随时告诉我是否不回答问题