Now in the function clone() I create a new Bacteria with dynamic allocated memory.
1 2 3 4 5 6 7 8 9 10 11 12
Bacteria *daughter_bacteria = new Bacteria(); //daughter_bacteria is a pointer to the newly created bacteria
// now make this bacteria alive
std::thread thread_bacteria_life (daughter_bacteria->life()); //Here I seemingly make some syntax mistakes
The compiler error I get is:
Error 1 error C2664: 'std::thread::thread(std::thread &&) throw()' : cannot convert parameter 1 from 'void' to 'std::thread &&' c:\users\reto\documents\visual studio 2012\projects\bacteria\bacteria\bacteria.cpp 101 1 Bacteria
2 IntelliSense: no instance of constructor "std::thread::thread" matches the argument list
argument types are: (void) c:\Users\Reto\Documents\Visual Studio 2012\Projects\Bacteria\Bacteria\Bacteria.cpp 101 36 Bacteria
You're calling your function and trying to pass its void output the thread's constructor, which will of course hiccup. Instead, you need to pass the actual function that you want the thread to execute.
Somehow I suspect that what you're doing with threads will end up disastrously. Do the bacteria delete themselves when the thread for their life functions is done executing? How many threads are you expecting to create? Have you tried single-threaded solutions first?
The error is: Debug error. R6010 -abort() has been called
I don't understand any of this. Can someone explain this, please?
Thanks Albatross for your remarks.
Well, Yes the bacterias are intended to somehow delete themselves when they die.
How many threads are supposed to be created?
As much as possible. Is there some limit?
Have I tried single-threaded solutions first?
No, because this should become some sort of a simulation where I think this multi-threaded programming style is the most appropriate approach.
I'm not sure what's causing your error because you tell us little to nothing about the program, but do read this.
At a certain point, using threads starts costing you more than it gets you. Using a multi-threaded approach will help you with computational performance, but keep in mind that your computer in all probability has no more than 2, 4 or in rare cases 6 or 8 physical cores (unless it has a server processor in which case it'll have more). You'll probably end up with 100s of bacteria, and your problem is probably I/O bound anyway (I/O bound = multithreading not recommended).
If you didn't get any part of what I said, then you should probably step back from multithreading. While there are cases when you'll be hard-pressed not to use it, this is definitely not one of them.
EDIT @giblit: Yes, some processors have 6 cores. Some processors