943,546 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 7013
  • C++ RSS
Jun 12th, 2008
0

Using CreateThread() function

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 15
Solved Threads: 3
Light Poster
Cybulski is offline Offline
47 posts
since Apr 2008
Jun 12th, 2008
0

Re: Using CreateThread() function

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,947 posts
since Aug 2005
Jun 12th, 2008
0

Re: Using CreateThread() function

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?
Reputation Points: 15
Solved Threads: 3
Light Poster
Cybulski is offline Offline
47 posts
since Apr 2008
Jun 12th, 2008
0

Re: Using CreateThread() function

Click to Expand / Collapse  Quote originally posted by Cybulski ...
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.
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Jun 12th, 2008
0

Re: Using CreateThread() function

Now I need to do something like this:
C++ Syntax (Toggle Plain Text)
  1. CreateThread( NULL, 0, &MultiReader::ReaderThread, (void*)vDirectories.at(nThreadNo), 0, NULL);

While vDirectories is:
C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 15
Solved Threads: 3
Light Poster
Cybulski is offline Offline
47 posts
since Apr 2008
Jun 12th, 2008
1

Re: Using CreateThread() function

The call
C++ Syntax (Toggle Plain Text)
  1. vDirectories.at(nThreadNo)
will return the reference to the element of the vector. If you will get it address
C++ Syntax (Toggle Plain Text)
  1. &vDirectories.at(nThreadNo)
and convert to the void pointer
C++ Syntax (Toggle Plain Text)
  1. (void*)&vDirectories.at(nThreadNo)
the call should be accepted by the compiler.

You can convert the passed pointer back in the thread function
C++ Syntax (Toggle Plain Text)
  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):
C++ Syntax (Toggle Plain Text)
  1. CreateThread( <blockquote>NULL,
  2. 0,
  3. boost::bind(&MultiReader::ReaderThread,this),
  4. (void*)&vDirectories.at(nThreadNo),
  5. 0,
  6. NULL);</blockquote>
Reputation Points: 11
Solved Threads: 2
Newbie Poster
writem is offline Offline
8 posts
since Jun 2008
Jun 12th, 2008
0

Re: Using CreateThread() function

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.
Reputation Points: 15
Solved Threads: 3
Light Poster
Cybulski is offline Offline
47 posts
since Apr 2008
Jun 13th, 2008
0

Re: Using CreateThread() function

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
Reputation Points: 15
Solved Threads: 3
Light Poster
Cybulski is offline Offline
47 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Quick question on Stacks, queue and heap
Next Thread in C++ Forum Timeline: selecting entire row in CListCtrl





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC