Providing "thread" inside class method

Hello everybody!

It's the first time that I write on this forum. I'm still a beginner on C++ world (i'm coming from Java, .NET and C#, this is the first experience with low level programming languages).

Lately I was trying to use C++ with classes (yeah... really hard to begin), then I decided to start writing a simple TCP multithread server.

It's quite all clear what i have to do, but there still something that i cannot understand with threads as shown in the code below:

1
2
3
4
5
6
7
8
9
10
11
void Srv::start()
{
	bool stop;

	thread tServer(serverThread, ref(stop));
}

void Srv::serverThread(bool param)
{
	//server's BL
}


I have a public class named 'Srv' within it there are quite lot of utilities methods called within 'serverThread()'. In 'start()' function, on the tServer's constructor declaration line, I get this error: "No instance of constructor "std::thread::thread" matches the argument list. Arugment type are (void (bool param))". If i try to put a simple method like...

1
2
3
4
void hello()
{
       //hello's BL
}


...and it's all right.

The goal that i want to reach is to have one function who invoke the server.

Sorry for my 'newbie incompetence', probably it could be solved in a few seconds, but I'm still learning and it's not simple for me now :)

I'll also provide server's BL if could be for help (don't care about italian words within cout and cerr, nothing offensive :) ):

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
56
57
58
59
60

void Srv::serverThread(bool param)
{
	LPDWORD thread = NULL;

	struct sockaddr_in srvAddr, client;

	srvAddr.sin_family = AF_INET;
	srvAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	srvAddr.sin_port = 27015;

        //getSck is a wrapper that simply return a SOCKET... nothing special...
	SOCKET sck = getSck(&srvAddr);

	int clientLength = sizeof(client);

	char clBuffer[1024];

	//--------------------------------------------------------------------------

	while (param){

		SOCKET clientAccepted = accept(sck, (sockaddr*)&client, &clientLength);

		if (clientAccepted == INVALID_SOCKET){
			cerr << "accept fallita con codice di errore: %ld\n";
                        cerr << WSAGetLastError();

			closesocket(clientAccepted);
			WSACleanup();
		}
		else {
			cout << "Client connesso" << endl;
		}
		
                //ha-ha! Another similar error here on 'reciver' callback, 
                //but i'm confident to find other ways!
		CreateThread(NULL, 0, reciver, (LPVOID)clientAccepted, 0, thread);

	}

	//-------------------------------------------------------------------------

	cout << "Spegnimento" << endl;
	int sckClosed = closesocket(sck);

	if (sckClosed == SOCKET_ERROR)
	{
		cout << "Non spento" << endl;
	}
	else
	{
		cout << "Spento" << endl;
		WSACleanup();
	}

	ExitThread(0);

	return NULL;
}


...and class header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Srv
{
private:
	pthread_t server, cmd;
	
	SOCKET getSck(sockaddr_in *srv);
	DWORD WINAPI reciver(LPVOID param);
	void serverThread(bool param);      //<----- 1
	void cmdThread(bool param);

public:
	Srv();
	Srv(pthread_t server, pthread_t cmd);

	void start();      //<------ 2

	pthread_t Srv::getCmdThread();
	void Srv::setCmdThread(pthread_t cmd);
	pthread_t getServerThread();
	void setServerThread(pthread_t server);
};


Will i provided all the necessary?
Hope someone will help a poor and hangry for C++ guy :)

Thankyou!

Regards,

Andrea
ref(stop)? &stop is used to pass stop by reference.

Aceix.
Let's start with the interface.

The user of your server class shouldn't need to know anything about threads. The decision to use thread, and the strategy of using threads are an internal matter to the class.

The goal that i want to reach is to have one function who invoke the server.
Now, this can be tricky.

The problem is, a server has state information. It needs to know who's connected, the state of the communication protocol(s), stuff about itself ... so that information should be held in a class somewhere.

It's helpful if we think how your one function is going to work. And also, once you've started the server, how to you maintain control of it if you have a fire and forget relationship with it?

In short, I don't think that's a reasonable model to take with a server.


So let's get back to your server interface. What did you have in mind with cmd?

Once we can define the interface, we can deal with the implementation, not the other way round.
Thankyou kbw: I understand that my strategy it's quite failure so... I would be grateful if you were available to suggest me a correct model to apply to make a generic TCP multithreaded server who accept incoming connections, because I'm quite confused and realized that now I've no idea on how to go forward on this. I thought with 'Java mind' and now I can understand that in C++ it's not as easy as I expected :)

...what I wanted to do with the 'cmd' process was to try to interact between the two processes using 'stop' reference variable but I saw in other cases that it doesn't work... don't care about that, just consider it a misprint of my previous code.

Answering to Aceix:
I followed this tutorial -> http://www.codeproject.com/Articles/598695/Cplusplus-threads-locks-and-condition-variables on thread class and a few days later I realized that was the same as using &stop.


It'd strongly recomend using the sockets library if you want to learn network programming. It's low level enough to help you understand the TCP protocol on the wire, and the notions involved.

If you were going to use networks in a program, I'd recomend you use a higher level library. Boost is fashionable these days. Here's a Boost TCP stream server example: http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/example/iostreams/daytime_server.cpp

The top level example page is here: http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/examples.html#boost_asio.examples.iostreams.

The notable features of the example are:
It ought to be portable.
It supports IPv4 and IPv6.
It supports the C++ iostream library, so you get a TCP stream that read/write with.
It's concise.

Ok, that is implementation, back to your design.


So are you saying that all you want is a means of stopping the server? i.e. you just want:
1
2
3
4
5
6
7
8
class TCPServer
{
public:
    // set listener end point

    void start();
    void stop();
};
Thankyou for providing me Boost, I'm sure that's quite complex to understand cause it has thousand and thousand of functionalities, that will be pretty hard to find what I really need -but- I'll take my time to try search for it.

Yeah, that's the beginning structure that I had in my mind... joining with also a 'server console' for sending commands to the server like:"Give me clients's list" or "Shut down" or the best one "Kill client 'xxx' " ecc...

I'd like to separate it in two parts (or threads):

Server - which handle multiple requests and start interact with clients.
Command (the famous Cmd thread) - which interact with the server and with clients connected to the server.

I have a single thread server perfectly working and think that I'll still use it, for now.

I just need to know how to 'put all things together' inside that class that you perfectly explained in your upper reply.

(I'll reply later on this thread because now I have to do some work. Keep in touch!)
Last edited on
Topic archived. No new replies allowed.