| | |
Using CreateThread() function
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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:
And I get this:
Can anyone point my error? Suggestion maybe?
C++ Syntax (Toggle Plain Text)
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 possibleCan anyone point my error? Suggestion maybe?
Now I need to do something like this:
While vDirectories is:
But
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?
C++ Syntax (Toggle Plain Text)
CreateThread( NULL, 0, &MultiReader::ReaderThread, (void*)vDirectories.at(nThreadNo), 0, NULL);
While vDirectories is:
C++ Syntax (Toggle Plain Text)
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?
•
•
Join Date: Jun 2008
Posts: 8
Reputation:
Solved Threads: 2
The call will return the reference to the element of the vector. If you will get it address and convert to the void pointer the call should be accepted by the compiler.
You can convert the passed pointer back in the thread function
Also there is a possibility to use the non-static function ReaderThread (needs to include the boost library to your project):
C++ Syntax (Toggle Plain Text)
vDirectories.at(nThreadNo)
C++ Syntax (Toggle Plain Text)
&vDirectories.at(nThreadNo)
C++ Syntax (Toggle Plain Text)
(void*)&vDirectories.at(nThreadNo)
You can convert the passed pointer back in the thread function
C++ Syntax (Toggle Plain Text)
DWORD WINAPI MultiReader::ReaderThread(LPVOID p_pVoid) {<blockquote>OPTIONS* parameter = (OPTIONS*)p_pVoid; //use structure by pointer</blockquote>}
Also there is a possibility to use the non-static function ReaderThread (needs to include the boost library to your project):
C++ Syntax (Toggle Plain Text)
CreateThread( <blockquote>NULL, 0, boost::bind(&MultiReader::ReaderThread,this), (void*)&vDirectories.at(nThreadNo), 0, NULL);</blockquote>
Everything ok with this function now, I mark this thread as solved and continue being problematic in this thread:
http://www.daniweb.com/forums/thread127041.html
http://www.daniweb.com/forums/thread127041.html
![]() |
Similar Threads
- Cant find right Message handle. (C++)
- multithreading c++ (C++)
- Multithreading error (C++)
- CreateThread & CreateRemoteThread (C++)
- putting the address of a function pointer into a struct (C)
- Help with multithread server (C)
Other Threads in the C++ Forum
- Previous Thread: Quick question on Stacks, queue and heap
- Next Thread: selecting entire row in CListCtrl
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






