开发者

Thread c++ Prevent value from changing

I am using boosthread to create 3 threads calling the same function each time wi开发者_开发技巧th different arguments being passed. E.g. 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)

When a global value within the thread is changed I do not want the other threads to execute and change the value again E.g function(a,b){

if(something happened for that thread) value = 5;

//if nothing happened value = 1; }

If one thread gets a value of 5 then I do not want the other threads to interfere with that value and make it go back to 1. How can I do this? Thanks.

Maybe the way to do this is to use boost:mutex but I do not see any gain in doing so because this value is found just before the return statement and I might well have used boost join_all(). But this would be less efficient.


I have one example that will help you. It uses C++11 but can be transposed to Boost easily.

the algorithm is simple, it is made of two main parts:

  1. Launches three threads
  2. Joins them(waits for them to finish)

What each thread does:

  1. Does some work
  2. Inititalizes a variable if it has never been initialized before (FirstThreadToFinish)

How does the unique intializer function works. 1.uses a mutex to prevent multiple access to shared variables (guarantees that ths function is only acceesed by one thread at a time) 2.If the variables has not been initialized it initializes it with the name of the thread and sets the boolean to true (variable initialized) 3. otherwise does nothing

#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;
}

Hope that helps, feel free to tell me if does not answer the question

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜