Using CreateThread() function

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2008
Posts: 47
Reputation: Cybulski is an unknown quantity at this point 
Solved Threads: 3
Cybulski's Avatar
Cybulski Cybulski is offline Offline
C++ wannabe

Using CreateThread() function

 
0
  #1
Jun 12th, 2008
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:
  1. void MultiReader::CreateThreads()
  2. {
  3. unsigned int nThreadNo;
  4.  
  5. for( nThreadNo = 0; nThreadNo < vDirectories.size(); nThreadNo++)
  6. {
  7. CreateThread( NULL, 0, &MultiReader::ReaderThread, vDirectories.at(nThreadNo).sDirectoryName, 0, NULL);
  8. }
  9. }
  10.  
  11. DWORD WINAPI MultiReader::ReaderThread(LPVOID p_pVoid)
  12. {
  13. return NULL;
  14. }

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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Using CreateThread() function

 
0
  #2
Jun 12th, 2008
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 47
Reputation: Cybulski is an unknown quantity at this point 
Solved Threads: 3
Cybulski's Avatar
Cybulski Cybulski is offline Offline
C++ wannabe

Re: Using CreateThread() function

 
0
  #3
Jun 12th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Using CreateThread() function

 
0
  #4
Jun 12th, 2008
Originally Posted by Cybulski View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 47
Reputation: Cybulski is an unknown quantity at this point 
Solved Threads: 3
Cybulski's Avatar
Cybulski Cybulski is offline Offline
C++ wannabe

Re: Using CreateThread() function

 
0
  #5
Jun 12th, 2008
Now I need to do something like this:
  1. CreateThread( NULL, 0, &MultiReader::ReaderThread, (void*)vDirectories.at(nThreadNo), 0, NULL);

While vDirectories is:
  1. struct OPTIONS
  2. {
  3. string sDirectoryName;
  4. vector<string> vExtensions;
  5. int uOperationId;
  6. };
  7.  
  8. 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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 8
Reputation: writem is an unknown quantity at this point 
Solved Threads: 2
writem writem is offline Offline
Newbie Poster

Re: Using CreateThread() function

 
1
  #6
Jun 12th, 2008
The call
  1. vDirectories.at(nThreadNo)
will return the reference to the element of the vector. If you will get it address
  1. &vDirectories.at(nThreadNo)
and convert to the void pointer
  1. (void*)&vDirectories.at(nThreadNo)
the call should be accepted by the compiler.

You can convert the passed pointer back in the thread function
  1. DWORD WINAPI MultiReader::ReaderThread(LPVOID p_pVoid)
  2. {<blockquote>OPTIONS* parameter = (OPTIONS*)p_pVoid;
  3. //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):
  1. CreateThread( <blockquote>NULL,
  2. 0,
  3. boost::bind(&MultiReader::ReaderThread,this),
  4. (void*)&vDirectories.at(nThreadNo),
  5. 0,
  6. NULL);</blockquote>
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 47
Reputation: Cybulski is an unknown quantity at this point 
Solved Threads: 3
Cybulski's Avatar
Cybulski Cybulski is offline Offline
C++ wannabe

Re: Using CreateThread() function

 
0
  #7
Jun 12th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 47
Reputation: Cybulski is an unknown quantity at this point 
Solved Threads: 3
Cybulski's Avatar
Cybulski Cybulski is offline Offline
C++ wannabe

Re: Using CreateThread() function

 
0
  #8
Jun 13th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC