Thread within class

I'm trying to use thread lib within my class, the class basically just runs specific python function and stores it's return value, everything works fine however while waiting for the python function to finish my program freezes, so I decided to execute the script in a thread, however I'm getting this error:

error C2276: '&': illegal operation on bound member function expression


the code for the functions is below, execute is public function, the one an user is supposed to call, call function is private, called only by execute

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
int PythonFunction::execute(bool make)
{
	std::thread thr(&call, make); //not using & gives me: non-standard syntax; use '&' to create a pointer to member
	thr.join();

	return callreturn;
}

void PythonFunction::call(bool make)
{
	if (pValue != NULL)
	{
		Py_DECREF(pValue);
		pValue = NULL;
	}

	if (make)
	{
		int res = makeTuple();
		if (res != PYTHON_ERROR_SUCCESS)
		{
			callreturn = res;
			return;
		}
	}

	pValue = PyObject_CallObject(pFunc, pTuple);
	if (!pValue)
	{
		callreturn = PYTHON_ERROR_FUNCTION;
		return;
	}

	callreturn = PYTHON_ERROR_SUCCESS;
}
Last edited on
> error C2276: '&': illegal operation on bound member function expression

std::thread is for normal functions only. There is one way around; you can use a normal function as a proxy and the proxy function will call whatever member function you need.

Function prototype :
static void call_proxy(bool make, PythonFunction *_this);

Function definition :
void PythonFunction::call_proxy(bool make, PythonFunction *_this) {return _this-> call(make);}

In PythonFunction::execute() :
1
2
3
std::thread thr(PythonFunction::call_proxy, make, this); 

thr.join();
Does that help you? :)
Change line 3:

std::thread thr(&PythonFunction::call, this, make);
coder777's solution works, but my program still freezes when I call the function, I guess it's not enough to call the thread within a class and hope that the GUI will continue, right? any idea how should I implement this in a better way, so I can use my program while waiting for the function return? i'm using WinAPI, calling this within BOOL CALLBACK MainDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)

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
									PythonFunction pf;
									if (pf.loadFunction("nao", "posture") == PYTHON_ERROR_SUCCESS)
									{
										pf.addArgument(PythonArgument("Stand"));
										int res = pf.execute(1);
										if (res != PYTHON_ERROR_SUCCESS)
										{
											stringstream ss;
											ss << "Calling function `posture` failed. ";

											Log(LOG, (char*)ss.str().c_str());
											break;
										}

										PyObject *pValue = pf.getValue();
										if (!pValue)
										{
											Log(LOG, "pValue == NULL");
											break;
										}

										stringstream ss;
										ss << "return: ";
										if (PyString_Check(pValue))
											ss << PyString_AsString(pValue);
										else if (PyInt_Check(pValue))
											ss << PyInt_AsLong(pValue);
										else if (PyLong_Check(pValue))
											ss << PyLong_AsLong(pValue);
										else if (PyFloat_Check(pValue))
											ss << PyFloat_AsDouble(pValue);

										Log(LOG, (char*)ss.str().c_str());
									}
								}
Yes, it still freezes because line 4 will block like a normal function call would do.

One way to solve this is sending a private message (and process the result there) when the thread is done. See:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms644950(v=vs.85).aspx

https://msdn.microsoft.com/en-us/library/windows/desktop/ms644947(v=vs.85).aspx


You may also consider to process all the python stuff within the parallel thread and sending messages when there is something to do for the gui.
Topic archived. No new replies allowed.