类中的创建线程错误:类型'DWORD (windows_thread::)(void*)'的参数与"DWORD (*)(void*)"不匹配?
CreateThread inside a class error: argument of type 'DWORD (windows_thread::)(void*)' does not match 'DWORD (*)(void*)'?
我试图在类内创建一个线程,但遇到了错误。我只是想知道是否有办法解决这个问题,或者我必须仅在int main()
中使用线程?
C:windows_thread_2windows_thread_2.cpp||In member function 'void windows_thread::thread()':|
C:windows_thread_2windows_thread_2.cpp|24|error: argument of type 'DWORD (windows_thread::)(void*)' does not match 'DWORD (*)(void*)'|
C:windows_thread_2windows_thread_2.cpp||In member function 'DWORD windows_thread::Thread_no_1(void*)':|
C:windows_thread_2windows_thread_2.cpp|70|warning: deprecated conversion from string constant to 'char*'|
||=== Build finished: 1 errors, 1 warnings ===|
这是代码。
#include <windows.h>
//#include <strsafe.h>
#include <stdio.h>
#define BUF_SIZE 255
class windows_thread
{
//-------------------------------------------------------------------
// A function to Display the message indicating in which tread we are
//-------------------------------------------------------------------
public:
windows_thread(){}
~windows_thread(){}
void thread()
{
int Data_Of_Thread_1 = 1; // Data of Thread 1
HANDLE Handle_Of_Thread_1 = 0; // variable to hold handle of Thread 1
HANDLE Array_Of_Thread_Handles[1]; // Aray to store thread handles
// Create thread 1.
Handle_Of_Thread_1 = CreateThread( NULL, 0, Thread_no_1, &Data_Of_Thread_1, 0, NULL);
if ( Handle_Of_Thread_1 == NULL) ExitProcess(Data_Of_Thread_1);
// Store Thread handles in Array of Thread Handles as per the requirement of WaitForMultipleObjects()
Array_Of_Thread_Handles[0] = Handle_Of_Thread_1;
// Wait until all threads have terminated.
WaitForMultipleObjects( 1, Array_Of_Thread_Handles, TRUE, INFINITE);
printf("Since All threads executed lets close their handles n");
// Close all thread handles upon completion.
CloseHandle(Handle_Of_Thread_1);
}
void DisplayMessage (HANDLE hScreen, char *ThreadName, int Data, int Count)
{
TCHAR msgBuf[BUF_SIZE];
size_t cchStringSize;
DWORD dwChars;
// Print message using thread-safe functions.
//StringCchPrintf(msgBuf, BUF_SIZE, TEXT("Executing iteration %02d of %s having data = %02d n"), Count, ThreadName, Data);
//StringCchLength(msgBuf, BUF_SIZE, &cchStringSize);
WriteConsole(hScreen, msgBuf, cchStringSize, &dwChars, NULL);
Sleep(1000);
}
//-------------------------------------------
// A function that represents Thread number 1
//-------------------------------------------
DWORD WINAPI Thread_no_1( LPVOID lpParam )
{
int Data = 0;
int count = 0;
HANDLE hStdout = NULL;
// Get Handle To screen. Else how will we print?
if( (hStdout = GetStdHandle(STD_OUTPUT_HANDLE)) == INVALID_HANDLE_VALUE )
return 1;
// Cast the parameter to the correct data type passed by callee i.e main() in our case.
Data = *((int*)lpParam);
for (count = 0; count <= 4; count++ )
{
DisplayMessage (hStdout, "Thread_no_1", Data, count);
}
return 0;
}
};
int main()
{
windows_thread threading;
threading.thread();
return 0;
}
我根据答案编辑了代码,如果您可以解决此错误,请对其进行编辑。 line 78 error: no matching function for call to 'windows_thread::Thread_no_1()'|
#include <windows.h>
//#include <strsafe.h>
#include <stdio.h>
#define BUF_SIZE 255
class windows_thread
{
//-------------------------------------------------------------------
// A function to Display the message indicating in which tread we are
//-------------------------------------------------------------------
public:
windows_thread(){}
~windows_thread(){}
void thread()
{
int Data_Of_Thread_1 = 1; // Data of Thread 1
HANDLE Handle_Of_Thread_1 = 0; // variable to hold handle of Thread 1
HANDLE Array_Of_Thread_Handles[1]; // Aray to store thread handles
// Create thread 1.
Handle_Of_Thread_1 = CreateThread( NULL, 0, Wrap_Thread_no_1, &Data_Of_Thread_1, 0, NULL);
if ( Handle_Of_Thread_1 == NULL) ExitProcess(Data_Of_Thread_1);
// Store Thread handles in Array of Thread Handles as per the requirement of WaitForMultipleObjects()
Array_Of_Thread_Handles[0] = Handle_Of_Thread_1;
// Wait until all threads have terminated.
WaitForMultipleObjects( 1, Array_Of_Thread_Handles, TRUE, INFINITE);
printf("Since All threads executed lets close their handles n");
// Close all thread handles upon completion.
CloseHandle(Handle_Of_Thread_1);
}
void DisplayMessage (HANDLE hScreen, char *ThreadName, int Data, int Count)
{
TCHAR msgBuf[BUF_SIZE];
size_t cchStringSize;
DWORD dwChars;
// Print message using thread-safe functions.
//StringCchPrintf(msgBuf, BUF_SIZE, TEXT("Executing iteration %02d of %s having data = %02d n"), Count, ThreadName, Data);
//StringCchLength(msgBuf, BUF_SIZE, &cchStringSize);
WriteConsole(hScreen, msgBuf, cchStringSize, &dwChars, NULL);
Sleep(1000);
}
//-------------------------------------------
// A function that represents Thread number 1
//-------------------------------------------
DWORD WINAPI Thread_no_1( LPVOID lpParam )
{
int Data = 0;
int count = 0;
HANDLE hStdout = NULL;
// Get Handle To screen. Else how will we print?
if( (hStdout = GetStdHandle(STD_OUTPUT_HANDLE)) == INVALID_HANDLE_VALUE )
return 1;
// Cast the parameter to the correct data type passed by callee i.e main() in our case.
Data = *((int*)lpParam);
for (count = 0; count <= 1; count++ )
{
//DisplayMessage (hStdout, "Thread_no_1", Data, count);
}
return 0;
}
static DWORD WINAPI Wrap_Thread_no_1( LPVOID lpParam )
{
windows_thread *self = reinterpret_cast<windows_thread*>(lpParam);
self->Thread_no_1();
}
};
int main()
{
windows_thread threading;
threading.thread();
return 0;
}
线程
必须是"纯函数",而不是对类的非静态成员函数的调用 - 这些需要为"this-pointer"提供额外的"隐藏"参数。
由于到目前为止您的代码没有使用任何成员变量[据我所知],因此简单的解决方案是使函数static
。
如果要使用类的成员变量或成员函数,则需要有一个静态函数并将对象(this
(作为参数的一部分传递给线程,然后使用reinterpret_cast<windows_thread*>(arg);
将其转换回来。
大致如下:
...
Handle_Of_Thread_1 = CreateThread( NULL, 0, Wrap_Thread_no_1, this, 0, NULL);
...
static DWORD WINAPI Wrap_Thread_no_1( LPVOID lpParam )
{
windows_thread *self = reinterpret_cast<windows_thread*>(lpParam);
self->Thread_no_1();
}
我已经删除了你原来的论点。假设线程每个对象只运行一个线程,则可以将其设置为成员变量。如果您需要具有单个参数的多个线程,则需要将传入的参数设为struct
或class
,并reinterpret_cast
,并将this
指针存储为该struct
或class
的一部分[然后可以windows_thread*
,因此不需要强制转换]。
您正在尝试将指向成员的指针传递给线程,这与此处的原始指针不同。 它需要一个对象附加到它。
Handle_Of_Thread_1 = CreateThread( NULL, 0, Thread_no_1, &Data_Of_Thread_1, 0, NULL);
通过更改以下内容使函数static
:
DWORD WINAPI Thread_no_1( LPVOID lpParam )
对此:
static DWORD WINAPI Thread_no_1( LPVOID lpParam )
。它(正确(指出此函数不需要this
.
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 在c++类上调用void函数
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- 在派生函数中指定void*参数
- C++为什么尽管我调用了void函数,它却不起作用
- 如何从void函数输出字符串
- 我应该使用什么来代替void作为变体中的替代类型之一
- 奇怪的结构&GCC&clang(void*返回类型)
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 为什么这个函数将"const char*"转换为"void* const"而不是"const void*"
- 引用一个已擦除类型(void*)的指针
- 将尾部调用void(i32,..)位转换为llvm::函数以获取FnAttribute
- 库函数需要一个 std::function<void(void)>,如何传入类函数?
- 如何将指针从一个void函数传递到另一个C++
- 为什么我在使用void函数时得到错误代码C2276
- void*到驱动程序中的UnicodeString
- 为什么在逗号分隔符上下文中将预增量的结果强制转换为void
- 指针没有更新它在void函数内部指向的值
- 类中的创建线程错误:类型'DWORD (windows_thread::)(void*)'的参数与"DWORD (*)(void*)"不匹配?
- 为什么需要类型铸件(UINT)(void*)(DWORD)