Click to See Complete Forum and Search --> : My thread class not working


hkBattousai
December 24th, 2009, 03:10 AM
Error given : Error 1 error C3867: 'GenericThread::ThreadProc': function call missing argument list; use '&GenericThread::ThreadProc' to create a pointer to member ...\GenericThread\GenericThread.cpp 19

IDE : MS Visual Studio 2005

GenericThread.h#pragma once
#include <Windows.h>

class GenericThread
{
public:
GenericThread();
~GenericThread();

// Create and run the thread
void Start(LPVOID lpParameter, int nPriority = 0, bool create_suspended = false, SIZE_T stack_size = 0, bool reserve_stack = false);

// This member function will run when you call StartThread()
DWORD WINAPI ThreadProc(LPVOID lpParameter);

// -15 : lowest; +15 : highest
void SetPriority(int nPriority);

// Get currently assigned priority level
int GetPriority();

// Get thread status
bool IsRunning();

// Pause execution
void Suspend();

// Resume execution
void Resume();

// Terminate execution
void Terminate();

// Return thread handle
HANDLE GetHandle();

private:
// Handle to the thread
HANDLE m_hThread;

// The thread id assigned by Windows, there is no use for now
LPDWORD m_lpdwThreadID;

// You can use this property to signal the ThreadProc() to prepare for termination
bool m_bExiting;

// Thread status
bool m_bRunning;
};

GenericThread.cpp#include "GenericThread.h"

GenericThread::GenericThread()
{
m_bExiting = false;
m_bRunning = false;
}

GenericThread::~GenericThread()
{
if (m_bRunning) CloseHandle(m_hThread);
}

void GenericThread::Start(LPVOID lpParameter, int nPriority /*= 0*/, bool create_suspended /*= false*/, SIZE_T stack_size /*= 0*/, bool reserve_stack /*= false*/)
{
DWORD dwCreationFlags = 0;
if (create_suspended) dwCreationFlags |= CREATE_SUSPENDED;
if (reserve_stack) dwCreationFlags |= STACK_SIZE_PARAM_IS_A_RESERVATION;
m_hThread = CreateThread(NULL, stack_size, ThreadProc, lpParameter, dwCreationFlags, m_lpdwThreadID); // ERROR GIVEN ON THIS LINE
SetPriority(nPriority);
m_bRunning = true;
}

DWORD WINAPI GenericThread::ThreadProc(LPVOID lpParameter)
{
while (true) Sleep(60000);
return 0;
}

void GenericThread::SetPriority(int nPriority)
{
SetThreadPriority(m_hThread, nPriority);
}

int GenericThread::GetPriority()
{
return GetThreadPriority(m_hThread);
}

bool GenericThread::IsRunning()
{
return m_bRunning;
}

void GenericThread::Suspend()
{
SuspendThread(m_hThread);
}

void GenericThread::Resume()
{
ResumeThread(m_hThread);
}

void GenericThread::Terminate()
{
CloseHandle(m_hThread);
m_bRunning = false;
}

HANDLE GenericThread::GetHandle()
{
return m_hThread;
}

hoxsiew
December 24th, 2009, 03:35 PM
#pragma once
#include <Windows.h>

class GenericThread
{
public:
GenericThread();
~GenericThread();

// Create and run the thread
void Start(LPVOID lpParameter, int nPriority = 0, bool create_suspended = false, SIZE_T stack_size = 0, bool reserve_stack = false);

// This member function will run when you call StartThread()
static DWORD WINAPI ThreadProc(LPVOID lpParameter);
//...

hkBattousai
December 24th, 2009, 03:47 PM
#pragma once
#include <Windows.h>

class GenericThread
{
static DWORD WINAPI ThreadProc(LPVOID lpParameter);

Yeah, it mysteriously works when I add the "static" keyword.
But why, what is the logic behind this?

hoxsiew
December 24th, 2009, 03:54 PM
Without it, since your ThreadProc function is a member of the GenericThread class, it has an implicit "this" pointer as the first parameter. As such, it doesn't match the function argument for CreateThread.