I'm trying to call this function requested times. All functions and variables are members of one class, access is public. So, I'm calling like this:

void MultiReader::CreateThreads()
{
	unsigned int nThreadNo;

	for( nThreadNo = 0; nThreadNo < vDirectories.size(); nThreadNo++)
	{
		CreateThread( NULL, 0, &MultiReader::ReaderThread, vDirectories.at(nThreadNo).sDirectoryName, 0, NULL);
	}
}

DWORD WINAPI MultiReader::ReaderThread(LPVOID p_pVoid)
{
	return NULL;
}

And I get this:

error C2664: 'CreateThread' : cannot convert parameter 3 from 'DWORD (__stdcall MultiReader::* )(PVOID)' to 'LPTHREAD_START_ROUTINE'
        There is no context in which this conversion is possible

Can anyone point my error? Suggestion maybe?

Recommended Answers

All 7 Replies

two problems:
1) ReaderThread must be a static method of the class

2) remove the & &MultiReader::ReaderThread, . Like arrays all you need is the name of the method.

Yep, that's it. I tried to figure it out for days. & was there just because compiler suggested me to put it there. That thing is solved, thank you very much.

Now parameter 4: it should be &adress of this string, amirite?

Now parameter 4: it should be &adress of this string, amirite?

Basically a pointer (cast to void *) to anything you want the thread routine to receive via p_pVoid argument.

Now I need to do something like this:

CreateThread( NULL, 0, &MultiReader::ReaderThread, (void*)vDirectories.at(nThreadNo), 0, NULL);

While vDirectories is:

struct OPTIONS
	{
		string sDirectoryName;
		vector<string> vExtensions;
		int uOperationId;
	};

	vector<OPTIONS> vDirectories;

But error C2440: 'type cast' : cannot convert from 'DirectoryReader::OPTIONS' to 'void *' All I can cast to (void*) is sDirectoryName.c_str(), even casting integer results in runtime error. Shouldn't be structure fully capable of casting to void type?

The call

vDirectories.at(nThreadNo)

will return the reference to the element of the vector. If you will get it address

&vDirectories.at(nThreadNo)

and convert to the void pointer

(void*)&vDirectories.at(nThreadNo)

the call should be accepted by the compiler.

You can convert the passed pointer back in the thread function

DWORD WINAPI MultiReader::ReaderThread(LPVOID p_pVoid)
{
[INDENT]OPTIONS* parameter = (OPTIONS*)p_pVoid;
//use structure by pointer[/INDENT]
}

Also there is a possibility to use the non-static function ReaderThread (needs to include the boost library to your project):

CreateThread( 
[INDENT]NULL, 
0, 
boost::bind(&MultiReader::ReaderThread,this),
(void*)&vDirectories.at(nThreadNo), 
0, 
NULL);[/INDENT]
commented: Yes! That's answer I needed. +1

I managed to make compiler accept parameter using &vDirectories.at(nThreadNo) but I missed that litte (void*). Thank you very much, good sir!

Threading almost solved for me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.