Boost Threading iIssue

closed account (2NywAqkS)
When I put boost::thread Thread; in my struct I get the error error C2248: 'boost::thread::thread' : cannot access private member declared in class 'boost::thread'

The whole struct is
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
struct Player
{
	bool key[256];
	char nameString[64];
	bool lMouseButton;
	bool rMouseButton;
	double mouseX;
	double mouseY;
	double direction;

	Vector position;
	Vector nextPosition;

	Vector velocity;

	Vector lastAcceleration;
	Vector newAcceleration;
	Vector avgAcceleration;

	bool yCollision, facingLeft, fired;

	double mass;

	int walkingFrame, lastWalkingFrameTime;

	std::vector <Bullet> bulletVector;

	sockaddr socketAddr;

	template <class Archive>
	void serialize(Archive & ar, const unsigned int version)
    {
		ar & key;
		ar & nameString;
		ar & lMouseButton;
		ar & rMouseButton;
		ar & mouseX;
        ar & mouseY;
		ar & nextPosition;
		ar & velocity;
		ar & bulletVector;
    }

	// Standard Functions
	Player();
	void Calculate(int delta);
	void Draw();

	// Socket
	void UpdateClient();

	// Threading
	void PlayerThreadFunction();
	boost::thread Thread;
};
You can't default construct a boost::thread because the constructor needs a callback in order to launch the thread.

If you're going to have a thread as a member var you'll have to explicitly construct it in your constructor's initialization list.

Or... you can own a pointer to a thread and create it with new when you're ready for the thread to start. To avoid having to delete, you can put it in a unique_ptr:

1
2
3
4
5
std::unique_ptr< boost::thread >  Thread; // or boost::unique_ptr if your compiler doesn't
   //  support C++11 smart pointers

// then in the in ctor... or wherever you want to create your thread:
Thread = std::unique_ptr<boost::thread>( new boost::thread( your_callback ) );





...

Of course... you probably should not be using a thread here in the first place....
Topic archived. No new replies allowed.