【问题标题】:Thread c++ Prevent value from changing线程 c++ 防止值改变
【发布时间】:2011-04-25 14:07:57
【问题描述】:

我正在使用 boostthread 创建 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){

如果(该线程发生了某事) value = 5;

//如果什么都没发生 值 = 1; }

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

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

【问题讨论】:

  • 尝试将更改后的值放入队列中。插入时确保它不存在于队列中,如果存在,则丢弃新获得的值,这样您将获得一个不应更改的值,并注意队列必须在锁定语句中,即 mutex
  • np 伴侣。希望它对你有用。

标签: c++ multithreading boost mutex


【解决方案1】:

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

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

  1. 启动三个线程
  2. 加入他们(等待他们完成)

每个线程的作用:

  1. 有什么作用
  2. 如果变量之前从未初始化过,则初始化变量 (FirstThreadToFinish)

独特的初始化函数是如何工作的。 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;
}

希望对你有帮助,如果没有回答问题,请随时告诉我

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    • 2018-03-03
    • 2015-12-25
    • 1970-01-01
    相关资源
    最近更新 更多