CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: two threads with shared memory

  1. #1
    Join Date
    Oct 2015
    Posts
    8

    two threads with shared memory

    I am very stuck and tired of reading and searching. I am not longer sure what I am doing, I have deleted and waited a lot, but this is the last i have written, where i haven't even started on the treads.

    I am trying to program a program i c++. First i need to implement animals from a figure, then i need to create a thread that create new objects and fills it with data as weight, length, where it was found,etc. this will be saved in a data-structure, example a vector in the shared memory. Last i need to create a thread that is able to see that data that is registered about the animals.

    Code:
    #include <iostream>
    #include <vector>
    #include <thread>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        class rovdyr
        {
        public:
            rovdyr();
            rovdyr(const rovdyr& r);
            virtual ~rovdyr();
            void setArt (const string& a);
            string getArt() const;
            
            void setFinding (const string& f);
            string getFinding() const;
            
            void setWeight (const double& w);
            string getWeight() const;
    
            void setAge (const double& g);
            string getAge() const;
            
            
            virtual void write();
            
        protected:
            string Art;
            string Finding;
            double Weight;
            double Age;
    
    };
        
        class landrovdyr : public rovdyr
        {
            
        };
        
        class seler : public rovdyr
        {
            
        };
        
        
        class hunder : public landrovdyr
        {
            
        };
        
        class bjørner : public landrovdyr
        {
            
        };
        
        class katter : public landrovdyr
        {
            
        };
        
        
        class øreseler : public seler
        {
            
        };
        
        class hvalrosser : public seler
        {
            
        };
        
        class selen : public seler
        {
            
        };
        
        
        
        class dingo : public hunder
        {
            
        };
        
        class ulv : public hunder
        {
            
        };
        
        
        class brunbjørn : public bjørner
        {
            
        };
        class grizzlybjørn : public bjørner
        {
            
        };
        class kodabjørn : public bjørner
        {
            
        };
        
        
        class løve : public katter
        {
            
        };
        
        class tiger : public katter
        {
            
        };
        
        class gerpard : public katter
        {
            
        };
        
        
        class sjøløve : public øreseler
        {
            
        };
        
        class  sjøbjørn : public øreseler
        {
            
        };
        
        
        class hvalross : public hvalrosser
        {
            
        };
        
        class østlighvalross : public hvalrosser
        {
            
        };
        
        
        class kobbe : public seler
        {
            
        };
        
        class havert : public seler
        {
            
        };
        
        class klappmyss : public seler
        {
            
        };

  2. #2
    Join Date
    Mar 2001
    Posts
    2,527

    Re: two threads with shared memory

    You need to start simple before you get to threads and shared
    memory and all these advanced programming constructs.

    This particular example shows correct syntax.
    http://www.cplusplus.com/doc/tutorial/classes/

    Try some tutorials:
    http://www.cplusplus.com/doc/tutorial/

    Work through them, compile them, run them.

    Happy coding!
    ahoodin
    To keep the plot moving, that's why.

  3. #3
    Join Date
    Oct 2015
    Posts
    8

    Re: two threads with shared memory

    Thank you for replying and the guiding

    This assignment as doe today so guess I just have to deliver what i got

  4. #4
    Join Date
    Mar 2001
    Posts
    2,527

    Re: two threads with shared memory

    You can't define classes in your main.

    member functions should not have the word class in front of them.

    READ the example and see how it is done. They are basic simple
    atomic examples that answer the questions you were asking.
    ahoodin
    To keep the plot moving, that's why.

  5. #5
    Join Date
    Oct 2015
    Posts
    8

    Re: two threads with shared memory

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

    using namespace std;

    mutex mtx;


    class rovdyr
    {
    public:
    string Art;
    string Finding;
    double Weight;
    double Length;
    double Age;
    };

    class landrovdyr : public rovdyr
    {

    };

    class seler : public rovdyr
    {

    };


    class hunder : public landrovdyr
    {

    };

    class bjorner : public landrovdyr
    {

    };

    class katter : public landrovdyr
    {

    };


    class oreseler : public seler
    {

    };

    class hvalrosser : public seler
    {

    };

    class selen : public seler
    {

    };



    class dingo : public hunder
    {

    };

    class ulv : public hunder
    {

    };


    class brunbjorn : public bjorner
    {

    };
    class grizzlybjorn : public bjorner
    {

    };
    class kodabjorn : public bjorner
    {

    };


    class love : public katter
    {

    };

    class tiger : public katter
    {

    };

    class gerpard : public katter
    {

    };


    class sjolove : public oreseler
    {

    };

    class sjobjorn : public oreseler
    {

    };


    class hvalross : public hvalrosser
    {

    };

    class ostlighvalross : public hvalrosser
    {

    };


    class kobbe : public seler
    {

    };

    class havert : public seler
    {

    };

    class klappmyss : public seler
    {

    };


    void feltbiolog()
    {
    vector<rovdyr> vec;
    lock_guard<mutex> guard(mtx);
    cout << "Jeg er feltbiolog" << endl;
    dingo *ding = new dingo;
    ulv *ulv2 = new ulv;
    brunbjorn *brunbj = new brunbjorn;
    grizzlybjorn *grizzbj = new grizzlybjorn;
    kodabjorn *kodabj = new kodabjorn;
    love *lion = new love;
    tiger *tigers = new tiger;
    gerpard *geopard = new gerpard;
    sjolove *sealion = new sjolove;
    sjobjorn *seabear = new sjobjorn;
    hvalross *hvalros2 = new hvalross;
    ostlighvalross *osthvalros = new ostlighvalross;
    kobbe *kob = new kobbe;
    havert *haver = new havert;
    klappmyss *klamyss = new klappmyss;

    ding->Art = "doggy";




    }

    void labbiolog()
    {

    lock_guard<mutex> guard(mtx);
    cout << "Jeg er labbiolog" << endl ;


    }



    int main()
    {

    thread thread1(feltbiolog);
    thread thread2(labbiolog);

    thread1.join();
    thread2.join();

    return 0;

    }

    // auto data = vector<double, string> {i = 0, i<100; i++};





    Is what i got now

  6. #6
    Join Date
    Mar 2001
    Posts
    2,527

    Re: two threads with shared memory

    ahoodin
    To keep the plot moving, that's why.

  7. #7
    Join Date
    Oct 2015
    Posts
    8

    Re: two threads with shared memory

    When i run it i get out:
    Feltbiolog
    Labbiolog
    Is that what you mean?

  8. #8
    Join Date
    Mar 2001
    Posts
    2,527

    Re: two threads with shared memory

    Yes. Good it runs. Its a start.

    Time to put in some research and work.
    ahoodin
    To keep the plot moving, that's why.

  9. #9
    Join Date
    Oct 2015
    Posts
    8

    Re: two threads with shared memory

    Yes i will work on set and get methods in objects now

  10. #10
    Join Date
    Oct 2015
    Posts
    8

    Re: two threads with shared memory

    I had this first but i got some help and I ended up with what i posted second.

    Code:
    #include <iostream>
    #include <vector>
    #include <thread>
    #include <string>
    #include <mutex>
    
    using namespace std;
    mutex mtx;
    
    
    class rovdyr
    {
    public:
        rovdyr(){}
        rovdyr(const rovdyr& r){}
        virtual ~rovdyr(){}
        
        void setArt (const string& a)
        {
            Art = a;
        }
        string getArt() const
        {
            return Art;
        }
        
        
        void setFinding (const string& f)
        {
            Finding = f;
        }
        
        string getFinding() const
        {
            return Finding;
            
        }
        
        
        void setWeight (const double& w)
        {
            Weight = w;
        }
        
        double getWeight() const
        {
            return Weight;
        }
        
        
        void setAge (const double& g)
        {
            Age = g;
        }
        double getAge() const
        {
            return Age;
        }
        
        void print()
        {
            unique_lock<::mutex> lck (mtx);
            cout << "Art: " << getArt() << " Age: " << getAge() << " Weight: " << getWeight() << " Finding " << getFinding();
        }
        
        void createObject(string a, string f, double w, double g)
        
        {
            Art = a;
            Finding = f;
            Weight = w;
            Age = g;
        }
        
        virtual void write(){}
        
        
    protected:
        string Art;
        string Finding;
        double Weight;
        double Age;
        
        
        
    };
    
    class landrovdyr : public rovdyr
    {
        
    };
    
    class seler : public rovdyr
    {
        
    };
    
    
    class hunder : public landrovdyr
    {
        
    };
    
    class bjørner : public landrovdyr
    {
        
    };
    
    class katter : public landrovdyr
    {
        
    };
    
    
    class øreseler : public seler
    {
        
    };
    
    class hvalrosser : public seler
    {
        
    };
    
    class selen : public seler
    {
        
    };
    
    
    
    class dingo : public hunder
    {
        
    };
    
    class ulv : public hunder
    {
        
    };
    
    
    class brunbjørn : public bjørner
    {
        
    };
    class grizzlybjørn : public bjørner
    {
        
    };
    class kodabjørn : public bjørner
    {
        
    };
    
    
    class løve : public katter
    {
        
    };
    
    class tiger : public katter
    {
        
    };
    
    class gerpard : public katter
    {
        
    };
    
    
    class sjøløve : public øreseler
    {
        
    };
    
    class  sjøbjørn : public øreseler
    {
        
    };
    
    
    class hvalross : public hvalrosser
    {
        
    };
    
    class østlighvalross : public hvalrosser
    {
        
    };
    
    
    class kobbe : public seler
    {
        
    };
    
    class havert : public seler
    {
        
    };
    
    class klappmyss : public seler
    {
        
    };
    
    void feltbiolog(vector<rovdyr>& vec)
    {
        
        cout << "Jeg er feltbiolog" << endl;
        landrovdyr a1;
        seler b1;
        hunder c1;
        bjørner d1;
        katter e1;
        dingo f1;
        ulv g1;
        brunbjørn h1;
        grizzlybjørn i1;
        kodabjørn j1;
        løve k1;
        tiger l1;
        gerpard m1;
        øreseler n1;
        hvalrosser o1;
        selen p1;
        sjøløve q1;
        sjøløve r1;
        hvalross s1;
        østlighvalross t1;
        kobbe u1;
        havert v1;
        klappmyss w1;
        
        
    }
    
    void labbiolog(vector<rovdyr>& vec)
    {
        cout << "Jeg er labbiolog" << endl;
    }
    
    int main()
    {
        unique_lock<::mutex> lck (mtx);
        vector<rovdyr> vec;
        thread thread1(feltbiolog, ref(vec));
        thread thread2(labbiolog, ref(vec));
        if (thread1.joinable())
        {
            thread1.join();
        }
        if (thread2 .joinable())
        {
            thread2.join();
        }
    
    }
    
    //auto data = vector<double, string> {i = 0, i<100; i++};

  11. #11
    Join Date
    Oct 2015
    Posts
    8

    Red face Re: two threads with shared memory

    void feltbiolog(vector<rovdyr>& vec)
    {

    cout << "Jeg er feltbiolog" << endl;
    landrovdyr a1;
    seler b1;
    hunder c1;
    bjørner d1;
    katter e1;
    dingo f1;
    ulv g1;
    brunbjørn h1;
    grizzlybjørn i1;
    kodabjørn j1;
    løve k1;
    tiger l1;
    gerpard m1;
    øreseler n1;
    hvalrosser o1;
    selen p1;
    sjøløve q1;
    sjøløve r1;
    hvalross s1;
    østlighvalross t1;
    kobbe u1;
    havert v1;
    klappmyss w1;

    a1.createObject(cin >>a);


    }


    I am trying to create a new object and i want cin in it but it doesn't work.. can you see what the problem is?

    Thank you very much for helping me! <3

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,318

    Re: two threads with shared memory

    Can you wrap your code with code tags? Use [ code] [/ code] minus the spaces.

  13. #13
    Join Date
    Oct 2015
    Posts
    8

    Re: two threads with shared memory

    Yes I can

    Code:
    #include <iostream>
    #include <vector>
    #include <thread>
    #include <string>
    #include <mutex>
    using namespace std;
    
    mutex mtx;
    
    
    class rovdyr
    {
    public:
        
        void setArt ( string a)
        {
            Art = a;
        }
        string getArt()
        {
            return Art;
        }
        
        
        void setFinding ( string f)
        {
            Finding = f;
        }
        
        string getFinding()
        {
            return Finding;
            
        }
        
        
        void setWeight (double w)
        {
            Weight = w;
        }
        
        double getWeight()
        {
            return Weight;
        }
        
        
        void setAge (int& g)
        {
            Age = g;
        }
        int getAge()
        {
            return Age;
        }
        
        void print()
        {
            cout << "Art: " << getArt() << " Age: " << getAge() << " Weight: " << getWeight() << " Finding " << getFinding() <<endl;
        }
        
        void CreateObject(string a, string f, double w, int g)
        
        {
            Art = a;
            Finding = f;
            Weight = w;
            Age = g;
        }
            
        
    protected:
        string Art;
        string Finding;
        double Weight;
        int Age;
        
        
    // Disse kunne vært i egene h og cpp filer men de er altså her.
    };
    
    class landrovdyr : public rovdyr
    {
        
    };
    
    class seler : public rovdyr
    {
        
    };
    
    
    class hunder : public landrovdyr
    {
        
    };
    
    class bjørner : public landrovdyr
    {
        
    };
    
    class katter : public landrovdyr
    {
        
    };
    
    
    class øreseler : public seler
    {
        
    };
    
    class hvalrosser : public seler
    {
        
    };
    
    class selen : public seler
    {
        
    };
    
    
    
    class dingo : public hunder
    {
        
    };
    
    class ulv : public hunder
    {
        
    };
    
    
    class brunbjørn : public bjørner
    {
        
    };
    class grizzlybjørn : public bjørner
    {
        
    };
    class kodabjørn : public bjørner
    {
        
    };
    
    
    class løve : public katter
    {
        
    };
    
    class tiger : public katter
    {
        
    };
    
    class gerpard : public katter
    {
        
    };
    
    
    class sjøløve : public øreseler
    {
        
    };
    
    class  sjøbjørn : public øreseler
    {
        
    };
    
    
    class hvalross : public hvalrosser
    {
        
    };
    
    class østlighvalross : public hvalrosser
    {
        
    };
    
    
    class kobbe : public seler
    {
        
    };
    
    class havert : public seler
    {
        
    };
    
    class klappmyss : public seler
    {
        
    };
    
    
    
    void AddObject(rovdyr r, vector<rovdyr>& vec)
    {
        lock_guard<mutex>shield(mtx);
        vec.push_back(r);
    }
    
    void feltbiolog(vector<rovdyr>& vec)
    {
        landrovdyr a1;
        seler b1;
        hunder c1;
        bjørner d1;
        katter e1;
        dingo f1;
        ulv g1;
        brunbjørn h1;
        grizzlybjørn i1;
        kodabjørn j1;
        løve k1;
        tiger l1;
        gerpard m1;
        øreseler n1;
        hvalrosser o1;
        selen p1;
        sjøløve q1;
        sjøløve r1;
        hvalross s1;
        østlighvalross t1;
        kobbe u1;
        havert v1;
        klappmyss w1;
        
    }
    
    void labbiolog(vector<rovdyr>& vec)
    {
        lock_guard<mutex> shield(mtx);
        for (int i = 0; i < vec.size(); i++);
        cout << "Jeg er labbiolog" << endl;
    }
    
    
    int main()
    {
        vector<rovdyr> vec;  // vektor for lagrede data
        
        thread thread1(feltbiolog, ref(vec)); // tråden for feltbiolog
        thread thread2(labbiolog, ref(vec)); //tråden for labbiolog
        
        thread1.join();
        thread2.join();
        
        return 0;
    }

  14. #14
    Join Date
    Oct 2015
    Posts
    3

    Re: two threads with shared memory

    I need help, I'm new here, how do I ask a question, I mean how do I start new post, I can reply but dotn know how to start new

  15. #15
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,318

    Re: two threads with shared memory

    Quote Originally Posted by khan786 View Post
    I need help, I'm new here, how do I ask a question, I mean how do I start new post, I can reply but dotn know how to start new
    You are in a thread. You need to pop up a level into one of the forums (where the list of threads are shown). From there, there will be a start new thread button in the upper left.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)