开发者

How do I pass an object to the CreateThread function?

I want to pass an object of a class as argument to the CreateThread function.

The object is created this way in main:

Connection *connection = new Bluetooth();
Brick *nxt = new Brick(connection);

The function that I pass to CreateThread is:

DWORD WINAPI recieveFunct(Brick nxt)

Finally, CreateThread is called in main:

IDRecieveThread = CreateThread(NULL, 0, recieveFunct, *nxt, 0, NULL);

In main I create the nxt object. This object has read and write functions that I want to call from the thread. However, I can't get it to work.

Do I need it to cast the object to HANDLE? Or am I looking completely wrong?


Thanks for your feedback guys!

I have implemented the solution given above like this:

int main()
{
  HANDLE IDRecieveThread = 0;

  //set up the NXT
 Connection *connection = new Bluetooth(); Brick *nxt = new Brick(connection);


  //Setup connection
  cout << "connecting..." << endl;
  connection->connect(3);
  cout << "connected" << endl;

  cout << "Starting recieve thread...." << endl;
  IDRecieveThread = CreateThread(NULL, 0, recieveFunct, reinterpret_cast<void*>(nxt),  0, 开发者_运维问答NULL);
  cout << "Recieve thread started" << endl;
}

But the compiler gives this error:

C:\Documents and Settings\Eigenaar\Bureaublad\BluetoothTestr\test\main.cpp||In function `int main()':|
C:\Documents and Settings\Eigenaar\Bureaublad\BluetoothTestr\test\main.cpp|39|error: invalid conversion from `void (*)(void*)' to `DWORD (*)(void*)'|
C:\Documents and Settings\Eigenaar\Bureaublad\BluetoothTestr\test\main.cpp|39|error:   initializing argument 3 of `void* CreateThread(_SECURITY_ATTRIBUTES*, DWORD, DWORD (*)(void*), void*, DWORD, DWORD*)'|
C:\Documents and Settings\Eigenaar\Bureaublad\BluetoothTestr\test\recievethread.h|4|warning: 'void recieveFunct(void*)' declared `static' but never defined|
||=== Build finished: 2 errors, 1 warnings ===|

Can someone tell me what is wrong? I'm getting the error when I call the CreateThread function.


You need to cast nxt to LPVOID, not HANDLE. MSDN says "LPVOID lpParameter [in, optional] - A pointer to a variable to be passed to the thread." This design pattern for passing data to a callback function or thread is commonly called a "user data" pointer.

http://msdn.microsoft.com/en-us/library/ms682453%28v=vs.85%29.aspx

You can just cast your Brick pointer to LPVOID (i.e. a pointer to void) when you call CreateThread() and uncast it in your recieveFunct thread func:

static void recieveFunct(void* pvBrick)
{
    Brick* brick = reinterpret_cast<Brick*>(pvBrick);
}

Connection *connection = new Bluetooth(); Brick *nxt = new Brick(connection);
IDRecieveThread = CreateThread(NULL, 0, recieveFunct, reinterpret_cast<void*>(nxt), 0, NULL);

Also, you might consider using beginthreadex() instead of CreateThread() if you are using the MSVCRT library. beginthreadex() will ensure that MSVCRT is properly initialized for your thread.


Use this signature:

DWORD WINAPI recieveFunct(void* pParam)
{
   Brick* pBrick = (Brick*)pParam; // Or whichever casting you prefer

  // Use the  `pBrick`

  return 0;
}

Create the thread as:

Brick *nxt = new Brick(connection);
CreateThread(NULL, 0, recieveFunct, (void*)nxt, 0, NULL);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜