how to get threads to communicate with shared memory ?

Hi,

I'm trying to write a code which allows me to get threads to communicate, a particular way.

I want these thread read and write from the same vector

how could that be done?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <fstream>
#include <istream>
#include <string>
#include <iostream>
#include <vector>
#include <future>

using namespace std;

class foo{
private:
    int n;
    string s;
public:
    foo(){}
    foo(string sp, int np):s(sp), n(np){}

    int getN(){ return this->n; }

    string getS(){ return this->s; }

    void setN(int np){ this->n = np; }

    static void display(vector<foo> p)
    {
        for(auto & i : p)
        {
            cout << i.getS() << i.getN() << endl;
        }
        cout << endl;
    }

    bool equalFoo(foo p)
    {
        if(this->n == p.getN() && this->s == p.getS())
            return true;
        else
            return false;
    }

    static void add(vector<foo> & p, foo l, int val){

        for (vector<foo>::size_type it = 0; it != p.size(); it++)
        {
            if(p[it].equalFoo(l))
            {
                p[it].setN(val);
                cout << "value changed !" << endl;
            }
        }
    }
};


int main()
{
    cout << "Hello World!" << endl;
    vector<foo> vectFoos =
    {
        {"S",1}, {"A",1}, {"A",2}, {"A",3}, {"A",4}, {"A",5}, {"A",6},
        {"S",2}, {"S",3}, {"B",1}, {"B",2}, {"B",3}, {"B",4}, {"B",5},
        {"B",6}, {"S",4}, {"S",5}, {"S",6}, {"C",1}, {"C",2}, {"C",3},
        {"C",4}, {"C",5}, {"C",6}, {"S",7}, {"S",8}, {"S",9}, {"S",10}
    };

    foo::display(vectFoos);

    auto outputRslt1 = std::async(foo::add, vectFoos, foo("B",2), 10);
    auto outputRslt2 = std::async(foo::add, vectFoos, foo("A",5), 10);
    auto outputRslt3 = std::async(foo::add, vectFoos, foo("C",1), 10);

    outputRslt1.get();
    outputRslt2.get();
    outputRslt3.get();

    foo::display(vectFoos);

    return 0;
}












Last edited on
Maybe something like this. Note the use of a mutex when modifying the vector. Also note the use of std::ref when passing vectFoos to std::thread.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// g++ -std=c++14 -o thread1 thread1.cpp -pthread

#include <iostream>
#include <string>
#include <vector>
#include <thread>
#include <mutex>

class Foo{
private:
    std::string s;
    int n;

    static std::mutex mtx;
public:

    Foo() : n(0) {}
    Foo(std::string sp, int np): s(sp), n(np) {}

    int getN() const { return this->n; }
    std::string getS() const { return this->s; }
    void setN(int np) { this->n = np; }

    static void display(std::vector<Foo> v) {
        for(const auto &f: v)
            std::cout << f.getS() << f.getN() << '\n';
        std::cout << '\n';
    }

    bool equalFoo(const Foo &f) {
        return this->n == f.getN() && this->s == f.getS();
    }

    static int add(std::vector<Foo> &v, Foo l, int val){
        for(auto &f: v)
            if (f.equalFoo(l)) {
                mtx.lock();
                f.setN(val);
                std::cout << "value changed!\n";
                mtx.unlock();
            }
        return 0;
    }
};

std::mutex Foo::mtx;

int main() {
    std::vector<Foo> vectFoos = {
        {"S",1}, {"A",1}, {"A",2}, {"A",3}, {"A",4}, {"A",5}, {"A",6},
        {"S",2}, {"S",3}, {"B",1}, {"B",2}, {"B",3}, {"B",4}, {"B",5},
        {"B",6}, {"S",4}, {"S",5}, {"S",6}, {"C",1}, {"C",2}, {"C",3},
        {"C",4}, {"C",5}, {"C",6}, {"S",7}, {"S",8}, {"S",9}, {"S",10}
    };

    Foo::display(vectFoos);

    std::thread t1(Foo::add, std::ref(vectFoos), Foo("B",2), 10);
    std::thread t2(Foo::add, std::ref(vectFoos), Foo("A",5), 10);
    std::thread t3(Foo::add, std::ref(vectFoos), Foo("C",1), 10);

    t1.join();
    t2.join();
    t3.join();

    Foo::display(vectFoos);
}

thank you @tpb
Topic archived. No new replies allowed.