We help IT Professionals succeed at work.

Problems with 'CreateThread()' - The 3rd paramter (thread function) generates an error I don't understand

John500
John500 asked
on
I'm getting the following error:

error C2664: 'CreateThread' : cannot convert parameter 3 from 'bool (class std::basic_string<char,struct std::char_traits<char>,class std::allocator <char> >)' to 'unsigned long (__stdcall *)(void *)'

I'm not sure how to correct this problem so that 'CreateThread()' accepts the 3rd parameter.  Here is a simplified version of the code:

bool MyFirstFunction(string Parameter)
{
...
...
return true;
}

bool MySecondFunction()
{
...
...
     hThread = CreateThread(
     NULL,     // no security attributes
     0,        // use default stack size
     MyFirstFunction,    // thread function
     MyArray[i],         // string argument to thread
     0,                  // use default creation flags
     &dwThreadId);       // returns the thread identifier
 
...
...
}
Comment
Watch Question

Commented:
Declare your function as :

DWORD WINAPI MyFirstFunction(LPVOID param)

Author

Commented:
nahumd,

I did what you said but the program wouldn't compile. Each place within MyFirstFunction() where the incoming string parameter was referenced is where the problems now occur because a string is expected.

When you say 'declare', do you mean in the function declaration (as I did) or within CreateProcess()?

Commented:
CreateThread() can only work with a function that accepts a void pointer and returns a DWORD.  So first, pass a pointer to the string.  Then, do some casting.

Code:

     DWORD MyFirstFunction(std::string* parameter)
     {
          // ...
          return true;
     }

     bool MySecondFunction()
     {
          std::string s("abc");
     
          DWORD dwThreadId;
          HANDLE hThread = CreateThread(
               NULL, 0,
               (LPTHREAD_START_ROUTINE)MyFirstFunction,
               &s, 0, &dwThreadId);
     
          // ...
          return 0;
     }

Commented:
For the C++ purists, write:

     reinterpret_cast<LPTHREAD_START_ROUTINE>(MyFirstFunction)

instead of the C-style cast.

Commented:
Note that I passed '&s' instead of 's'

Author

Commented:
alexo,

I'm not sure I understand you.  Why did you use the following syntax:

DWORD MyFirstFunction(std::string* parameter)


Did you mean to use
DWORD MyFirstFunction(VOID* parameter)

Also, you wouldn't happen to know the appropriate syntax to cast the 'parameter' into a string, or assign its value to a string?
   

Author

Commented:
alexo,

I have done all things as you mentioned but I have one error which says:

error C2664: 'CreateThread' : cannot convert parameter 4 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char>>' to 'void *'

I tried using an input parameter two different ways:

DWORD MyFirstFunction(std::string* parameter)
and
DWORD MyFirstFunction(void* parameter)

I get the same error above either way.  Thanks!

John







Author

Commented:
alexo,

My bad, I was able to fix it.  The problem was that I forgot to change the 4th parameter from 'S' to '&S'.

Can tell me how to cast the void pointer to a string pointer??

Thanks

Author

Commented:
alexo,

Wow, I'm really taking time catching up, but I think I understand now that I didn't have to change anything about the original function (the thread), I only had to cast the thread function as you specified.  The program compiles now.  Is that what you meant?

I'm clean out of points so even though I don't want to push my luck, I'm going to ask another related question :0

Do I have to do anything (is it a must) to end these threads individually or do I just let them run their course?

Commented:
John500, I'm sorry that I didn't get back to you but the email notification feature of EE wasn't working.  Anyway, it gave you the chance to figure things out by yourself...

>> Is that what you meant?
2 Changes: passing a pointer and casting.

Regarding "pushing your luck".  I would say that asking simple follow-up questions is acceptable.

To answer it: once the function exits, the thread terminates.  However, the function that created the thread may need to close the thread handle (it can do it even while the thread is running if the handle will not be used).

Explore More ContentExplore courses, solutions, and other research materials related to this topic.