Creating a thread with #include <thread>

Hi
I want to create thread, but I always have some compiler errors. Please help me.

I have a class called bacteria, here is the simplified class declaration

1
2
3
4
5
6
7
8
9
10
class Bacteria
{
private:
	char some_properties;
	void clone();
public:
	void life();
	Bacteria();
	~Bacteria(void);
};


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

#include "stdafx.h"
#include "Bacteria.h"
#include <ctime>
#include <thread> 

void Bacteria::clone()
{
	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

AND

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

I guess it should be
std::thread thread_bacteria_life (&Bacteria::life, daughter_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?

-Albatross
Thank you
 
std::thread thread_bacteria_life (&Bacteria::life, daughter_bacteria);

seems to work for the compiler.

However I get a runtime error now unless I call

 
thread_bacteria_life.join();


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.
Last edited on
[edit]
@schnuber:
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

-Albatross
Last edited on
There is also 6 core processors.
No I don't think my app is I/O bound
However I get a runtime error now unless I call
thread_bacteria_life.join();
The error is: Debug error. R6010 -abort() has been called

The destructor of std::thread executes terminate() if the thread hasn't been joined. Terminate calls abort.

PS: computers I work with run 1024 threads at the same time.. but I wouldn't have a bacteria per thread either, at least in the scenarios I can think of.
Last edited on
Topic archived. No new replies allowed.