简体   繁体   English

如何为同一个类对象的成员函数保留单独的变量副本?

[英]How can I keep separate variable copy for same class object's member function?

  • I have a class object obj1 and I am trying to call a member function sdf_write from 2-separate-threads. 我有一个类对象obj1 ,我试图从2个单独的线程调用成员函数sdf_write
  • There is a static variable wr_count inside the member-function. 在member-function中有一个静态变量wr_count

The issue is: when I run both threads, the wr_count value is being shared between both threads. 问题是:当我运行两个线程时,两个线程之间共享wr_count值。

For eg thread_1 runs 8-times and makes the wr_count=8 but when thread_2 starts it makes the wr_count=9 . 例如,thread_1运行8次并使wr_count = 8,但是当thread_2启动时,它使wr_count = 9 I want thread_2 to start counting from "1" not from the last value of thread_1. 我希望thread_2从“1”开始计数而不是从thread_1的最后一个值开始计数。

Here is my code: 这是我的代码:

#include <iostream>
#include <stdio.h>
#include <thread>
#include "sdf_func.hpp"
#include <vector>
using namespace std;
int main() {
    sdf obj1;
    std::thread t1([&obj1](){
        for (int i=0; i<30; i++) {
        while (!obj1.sdf_write(10));
        };
    });
    t1.detach();
    std::thread t2([&obj1](){
        for (int i=0; i<30; i++) {
        while (!obj1.sdf_write(10));
        };
    });
    t2.join();

    cout << "done: " << obj1.done << endl;

    // cout << "done: " << obj2.done << endl;

    // cout << "wr_count: " << obj1.wr_count << endl;
    return 0;   
}

// This is sdf_func/////////////////
#include <iostream>
#include <stdio.h>
#include <thread>
#include <mutex>
using namespace std;
class sdf {
    public:
    int done;
    std::mutex mutex;
    sdf() : done(0){};
    void increment() {
        std::lock_guard<std::mutex> guard(mutex);
        ++done;
    }
    bool sdf_write (auto size) {
        static int wr_count = 0;
        if (wr_count == size) {
            wr_count = 0;
            increment();
            //cout << "done : " << done;
            return false;
        }
        wr_count++;
        cout << wr_count << "--" << std::this_thread::get_id() << endl;
        return true;
    }
};

This is a perfect job for the thread_local storage duration, which is a keyword introduced from C++11. 这是thread_local存储持续时间的完美工作,这是从C ++ 11引入的关键字

thread_local int wr_count;

Essentially, you get a separate static instance of wr_count per thread; 实际上,每个线程都会获得一个单独的wr_count static实例; each one is initialised to 0 . 每个都初始化为0

Reference: http://en.cppreference.com/w/cpp/keyword/thread_local 参考: http//en.cppreference.com/w/cpp/keyword/thread_local

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

相关问题 如何获取指向类的复制构造函数的成员函数指针? - How can I obtain a member function pointer to the copy constructor of a class? 如何获得COM对象的成员函数的地址? - How can I get the address of a COM object's member function? 如何使具有包含唯一指针的成员变量的类可复制分配? - How can I make a class, with a member variable that contains unique pointers, copy assignable? How to access static class variable in static member function of same class? - How to access static class variable in static member function of same class? 如何在模板类中返回成员变量的副本? - How do I return a copy of a member variable in a template class? 我可以将类成员变量设置为类函数的结果吗? - Can I set a class member variable to the result of a class function? 如何从成员对象的函数访问对象中的变量 - How to access a variable in an object from a member object's function 如何使用该对象的 static 成员 function 初始化 object? - How do I initialize an object using a static member function of that object's class? 如何通过将类的对象和成员函数传递给C ++中的另一个函数来调用类? - How do I call a class by passing it's object and member function to another function in c++? 如何使类成员变量与函数模板的返回类型相同? - How to make a class member variable be the same type of return of a function template?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM