-
October 22nd, 2015, 06:33 PM
#1
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
{
};
-
October 23rd, 2015, 08:48 AM
#2
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.
-
October 23rd, 2015, 11:29 AM
#3
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
-
October 23rd, 2015, 11:35 AM
#4
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.
-
October 23rd, 2015, 11:48 AM
#5
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
-
October 23rd, 2015, 11:54 AM
#6
Re: two threads with shared memory
ahoodin
To keep the plot moving, that's why.
-
October 23rd, 2015, 12:04 PM
#7
Re: two threads with shared memory
When i run it i get out:
Feltbiolog
Labbiolog
Is that what you mean?
-
October 23rd, 2015, 12:21 PM
#8
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.
-
October 23rd, 2015, 01:07 PM
#9
Re: two threads with shared memory
Yes i will work on set and get methods in objects now
-
October 23rd, 2015, 01:11 PM
#10
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++};
-
October 23rd, 2015, 01:43 PM
#11
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
-
October 24th, 2015, 05:21 PM
#12
Re: two threads with shared memory
Can you wrap your code with code tags? Use [ code] [/ code] minus the spaces.
-
October 24th, 2015, 08:25 PM
#13
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;
}
-
October 24th, 2015, 10:50 PM
#14
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
-
October 25th, 2015, 07:35 AM
#15
Re: two threads with shared memory
Originally Posted by khan786
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|