gpt4 book ai didi

c++ - C++使用参数创建线程函数会导致转换错误

转载 作者:行者123 更新时间:2023-12-02 10:29:38 26 4
gpt4 key购买 nike

我在C++多线程中进行了第一步,遇到了一个问题。
我想实现的目标是设置带有将在单独线程中运行的参数的任务。
谷歌搜索答案并没有给我答案。
我正在使用的系统由Windows 10 Pro和MinGW 17.1的Code::Blocks 20.3组成
我创建了一个简单的示例来了解它。
第一个示例(单个cpp文件)可以正常工作。
但是,第二个(在一个类中)即使我使用相同的代码也有构建错误。
我希望有人能解释原因,并希望向我展示如何解决该问题。
第一个(有效的)示例:

#include <windows.h>
#include <process.h>
#include <iostream>
#include <chrono>
#include <thread>

struct Param
{
std::string param1;
};
unsigned Counter;

unsigned __stdcall DoSomething( void *ptr )
{
std::cout << "In second thread...\n";

Param *p_tmp = reinterpret_cast<Param*>(ptr);
Param p_param = *p_tmp;
std::string result = p_param.param1;

while ( Counter < 1000 )
{
Counter++;
std::cout << result << Counter << '\r';
std::this_thread::sleep_for(std::chrono::milliseconds(2));
}
std::cout << std::endl;
_endthreadex( 0 );

return 0;
}

void CreateThread()
{
HANDLE hThread;
unsigned threadID;
std::string result{"Count: "};
Param param;
param.param1 = "Count: ";

std::cout << "Main thread\nCreating second thread...\n";

// Create the second thread.
hThread = (HANDLE)_beginthreadex( NULL, 0, &DoSomething, (void*)&param, 0, &threadID );

// Wait until second thread terminates
WaitForSingleObject( hThread, INFINITE );

std::cout << "Returned to Main thread\nCounter should be 1000; it is-> "<< Counter << std::endl;
// Destroy the thread object.
CloseHandle( hThread );
}

int main()
{
CreateThread();
}
第二个示例(在一个类中):
struct Param
{
std::string param1;
};
unsigned Counter;

void CthreadFrame::CreateThread()
{
HANDLE hThread;
unsigned threadID;
Param param;
param.param1 = "Count: ";

std::cout << "Main thread\nCreating second thread...\n";

// Create the second thread
hThread = (HANDLE)_beginthreadex( NULL, 0, &DoSomething, (void*)&param, 0, &threadID );
// Wait for second thread to terminate
WaitForSingleObject( hThread, INFINITE );

std::cout << "Returned to Main thread\nCounter should be 1000; it is-> "<< Counter << std::endl;
// Destroy the thread object.
CloseHandle( hThread );

/* End of void CreateThread */
}

unsigned __stdcall CthreadFrame::DoSomething(void *ptr)
{
std::cout << "In second thread...\n";

Param *p_tmp = reinterpret_cast<Param*>(ptr);
Param p_param = *p_tmp;
std::string result = p_param.param1;

while ( Counter < 1000 )
{
Counter++;
std::cout << result << Counter << '\r';
std::this_thread::sleep_for(std::chrono::milliseconds(2));
}
std::cout << std::endl;
_endthreadex( 0 );

return 0;
}
建立讯息:
||=== Build: Debug in Cthread (compiler: GNU GCC Compiler) ===|
F:\Data\__C++\wxApps\Cthread\CthreadMain.cpp||In member function 'void CthreadFrame::CreateThread()':|
F:\Data\__C++\wxApps\Cthread\CthreadMain.cpp|114|error: cannot convert 'unsigned int (CthreadFrame::*)(void*)' to '_beginthreadex_proc_type' {aka 'unsigned int (*)(void*)'}|
f:\sdks\mingw-17.1\x86_64-w64-mingw32\include\process.h|37|note: initializing argument 3 of 'uintptr_t _beginthreadex(void*, unsigned int, _beginthreadex_proc_type, void*, unsigned int, unsigned int*)'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 2 second(s)) ===|

最佳答案

只需将您的线程函数声明为静态函数:

class CthreadFrame
{
public:
void Start() {
m_hThread = (HANDLE)_beginthreadex(
nullptr, 0, &CthreadFrame::DoSomething,
this /* store params here */, 0, &m_uThreadId
);
}

private:
static unsigned __stdcall DoSomething(void*);

private:
HANDLE m_hThread;
unsigned m_uThreadId;
};

关于c++ - C++使用参数创建线程函数会导致转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62858508/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com