Click to See Complete Forum and Search --> : How to Pass a Parameter to a Thread?


stephenprogrammer07
July 25th, 2007, 09:28 PM
Hi

I'm a newbie of sorts with C++ multithreading.

I am trying to figure out how to pass a char* to my Thread.

I'm using the following code.


class CFoo
{
public:
CFoo()
{
m_dwThreadID = 0;
m_hThread = 0;
}

~CFoo() { CloseHandle(m_hThread); }

bool Create()
{
m_hThread = CreateThread(0,
0,
ThreadFunc,
this,
0,
&m_dwThreadID);
if(!m_hThread)
{
// Could not create thread
return false;
}
return true;
}

HANDLE TheHandle(void) const { return m_hThread; }

private:
DWORD m_dwThreadID;
HANDLE m_hThread;

static DWORD WINAPI ThreadFunc(LPVOID pvParam);
};


DWORD WINAPI CFoo::ThreadFunc(LPVOID pvParam)
{
// Wait for 10 seconds.
for(int i = 0; i < 10; i++)
{
std::cout << "Start thread" << std::endl;
::Sleep(100);
}

return 0u;
}


I read that, the 4th parameter is how you pass a parameter to ThreadFunc.
But when I try to pass in a "test string" it doesn't compile.

m_hThread = CreateThread(0,
0,
ThreadFunc,
"test String",
0,
&m_dwThreadID);


Any thoughts ?

Thanks

Stev

mournerslament
July 25th, 2007, 09:58 PM
You need to cast whatever you send there as a void pointer.

To pass the string:


m_hThread = CreateThread(0,
0,
ThreadFunc,
(void *)"test String",
0,
&m_dwThreadID);

should do it.

stephenprogrammer07
July 25th, 2007, 10:00 PM
thanks

I never heard of that type before. Could you please tell me a little about it - the void pointer.

Stev

mournerslament
July 25th, 2007, 10:32 PM
Well, it is a pointer that is used to point at a memory address, without regard to type. It is useful in functions such as the CreateThread, because it lets you essentially pass any type or amount of information you want to a destination that knows how to use that memory. I believe it also lets you compare pointers to incompatible types...like int* and char* . You can't directly compare those two types of pointers normally, but you can if you cast one to a void pointer. Because a void* points at information without type, I also think that you can't do pointer arithmetic on a void*, nor can you dereference one. I am by no means an expert on them though, so if you want to know more/better information, if you post in the C++ forum and ask about it, I am sure that many very very smart people will be willing to tell you a lot about them.

Arjay
July 26th, 2007, 02:34 AM
Since you are passing the 'this' pointer in during thread creation, all you need to do is cast the LPVOID param back to the class type in your thread proc.

NOTE: I'm using string literal, so in this case it's okay to access the string returned by GetString(). However, if you accessed a string that could be written to by either thread, you'd want to protect the string with a critical section or some other synchronization object. See the articles in my subject line for more info.


class CFoo
{
private:
LPCTSTR GetString( ) { return _T("myString"); }

};

DWORD WINAPI CFoo::ThreadFunc(LPVOID pvParam)
{
CFoo* pFoo = (CFoo*)pvParam;

LPCTSTR sz = pFoo->GetString();
// Wait for 10 seconds.
for(int i = 0; i < 10; i++)
{
std::cout << "Start thread" << std::endl;
::Sleep(100);
}
return 0u;
}