Error Message w/ Multithreading

Hello Everyone,

Thank you for your time here. I need some assistance with multithreading for a homework assignment. I'm able to successfully use the _beginthread() function when threading a function in the same class as main(). However, if I create an object and call the function that way, it does not work. Appreciate your assistance. Here's the code I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
	MainClassThread mainThread;
	
	for(int i = 0; i < 3; i++)
	{
		// this line of code does NOT work
		_beginthread(mainThread.printMessage, 0, (void*)i);
		
		// this line of code DOES work if printMessage is in the same class as main()
		_beginthread(printMessage, 0, (void*)i);
	}

	Sleep(100);
}


The line of code that does NOT work generates the following error:
 
Error	1	error C3867: 'MainClassThread::printMessage': function call missing argument list; use '&MainClassThread::printMessage' to create a pointer to member

I think I found what I'm looking for. Can anyone confirm I'm barking up the right tree?

Here's the website under section "Using a C++ Member Function as the Thread's Entry-Point-Function"

http://www.codeproject.com/Articles/14746/Multithreading-Tutorial
Yes, pointer to member functions are odd and confusing to use. A good little database of knowledge on pointer-to-member functions is at http://www.parashift.com/c++-faq-lite/pointers-to-members.html - I recommend you read this. Of course, the option would be to use std::bind or std::function to bind the function to a temporary local function that can be passed properly to the function.

1
2
3
4
//...
for (int i = 0; i < 3; ++i) {
    _beginthread(std::bind(&MainClassThread::printMessage, mainThread, i), 0,nullptr);
}


EDIT:
Also, I know this is a homework assignment and you need to use the WinAPI thread functions, but you should probably look into std::thread anyway - the standardized thread utilities that should work on all platforms, not just windows.
Last edited on
Sounds good, thanks for the advise NT3. I have it working now. Cheers!
Topic archived. No new replies allowed.