Callback and static data member

Hi all,

I have a question regarding a callback that uses threads for a mysql connection. I can't access data member connectionPool in static function worker_thread. Is it possible or is there a way round this?

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

using namespace std;
using namespace mysqlpp;


T_Vectors::T_Vectors(Logger* log, const char* dbname, const char* host, const char* user, const char* password)
	: connectionPool(dbname, host, user, password), logger(log)
{
	cout << "Testing database connection..." << endl;
	Connection c(dbname, host, user, password);
	cout << "Connected!" << endl;
}

...

thread_return_t CALLBACK_SPECIFIER
	T_Vectors::worker_thread(thread_arg_t running_flag)
	{
	    mysqlpp::Connection::thread_start();

	    mysqlpp::Connection* cp = connectionPool->grab();

	    mysqlpp::Query query(cp->query("select * from users"));

      ....
        }


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

#define HAVE_THREADS
#define CALLBACK_SPECIFIER
typedef void* thread_return_t;
typedef void* thread_arg_t;

using namespace std;

class T_Vectors
{

public:
	T_Vectors(Logger* logger, const char* dbname, const char* host, const char* user, const char* password);
	static thread_return_t CALLBACK_SPECIFIER worker_thread(thread_arg_t running_flag);
	int tester();
	static int create_thread(thread_return_t(*worker)(thread_arg_t), thread_arg_t arg);

	~T_Vectors();

private:
	ConnectionPool connectionPool;
	Logger* logger;

...


Any help on the subject would be greatly appreciated, thanks

Yes you cannot use member data in static functions. The general way to implement thread functions in these scenarios is to send "this" pointer as the parameter to the thread function. Using the this pointer you can access the private members of the class.
Sorry for the late reply.
I'm still a bit confused - so I would have to create another "thread_arg_t arg2" parameter for the create_thread function which would be the "this" pointer ?

this is the create_thread definition
1
2
3
4
5
int T_Vectors::create_thread(thread_return_t(*worker)(thread_arg_t), thread_arg_t arg)
	{
		pthread_t pt;
		return pthread_create(&pt, 0, worker, arg);
	}


and in the main block I have:
1
2
3
4
5
...
for (i = 0; i < num_threads; ++i) {
       create_thread(worker_thread, running + i);
}
...

I'm a bit new to this! Thank you
your worker_thread static callback function should take a void* argument - that void* argument can be cast back to what you passed in as running + i on Line 3
you should convert the this pointer to void* and pass to the function pthread_create of the param arg,then in work_thread convert it back before using.

int T_Vectors::create_thread(thread_return_t(*worker)(thread_arg_t), thread_arg_t arg)
{
pthread_t pt;
return pthread_create(&pt, 0, worker, (void*) this);
}

T_Vectors::worker_thread(thread_arg_t running_flag)
{
.......
T_Vectors* pThis = ( T_Vectors* ) running_flag;
mysqlpp::Connection* cp = pThis ->connectionPool->grab();
........
}
Thank you for all the help!
I've solved it now :o)
Topic archived. No new replies allowed.