Passing object to static method

Hello experts!

I am using the function _beginthread to start a new thread. But its callback is static and I want to keep my processing in the class. So my question is: What is the best way to pass the object reference to the method serverProcessingInit So I can call its method serverProcessing which is not static.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef SERVER_H_
#define SERVER_H_

#include <iostream>
#include <process.h>
#include "WMap.h"
using namespace std;

class Server {
private:
	WMap *wMap = NULL;
	void serverProcessing();
public:
	Server();
	virtual ~Server();

	void initialize();
	static void serverProcessingInit(void *dummy);
};

#endif /* SERVER_H_ */ 


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
  #include "Server.h"

Server::Server() {
}

Server::~Server() {
}


void Server::initialize() {
	cout << "Initializing server..." << endl;

	this->wMap = new WMap();

	cout << "Activating server processing. (New Thread)" << endl;
	_beginthread(serverProcessingInit, 0, NULL);
	cout << "Server processing active." << endl;

	cout << "Activating sockets to listen to client connection" << endl;

	while(true) {

	}

	return;
}

void Server::serverProcessingInit(void *dummy) {
    //HERE I WANT TO CALL THE serverProcessing();
}

void Server::serverProcessing() {
	cout << "in" << endl;
	while(true) {
	}
}
Last edited on
Any reason why you can't use std::thread https://www.cplusplus.com/reference/thread/
Which at least knows about classes and can invoke class methods directly.

Or even portable POSIX threads.


> _beginthread(serverProcessingInit, 0, NULL);
Try
1
2
3
4
5
6
7
8
9
10
_beginthread(serverProcessingInit, 0, this);

...


void Server::serverProcessingInit(void *dummy) {
   Server *p = reinterpret_cast<Server*>(dummy);
   p->serverProcessing();
}


salem c, It worked perfectly. Thank you very much!
Topic archived. No new replies allowed.