• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
wanmaple

c++ A problem when create a thread in a class

10 posts in this topic

Dear everyone, I am writing a singleton class to manage a thread, here's the code:

#include <windows.h>

typedef class MsgListener
{
private:
    HANDLE _threadHandle;
    DWORD _threadId;
    static MsgListener *_instance;
    MsgListener();
    DWORD WINAPI Listen(LPVOID param);
public:
    static MsgListener *GetInstance();
    void StartListen();
    void StopListen();
} MSGLISTENER, *LPMSGLISTENER;

MsgListener *MsgListener::_instance = new MsgListener();
MsgListener::MsgListener() 
{ 
    _threadHandle = 0;
    _threadId = 0;
}
DWORD WINAPI MsgListener::Listen(LPVOID param) 
{ 
    // The method to run in a new thread....
}
MsgListener *MsgListener::GetInstance()
{
    if (!_instance)
        _instance = new MsgListener();
    return _instance;
}
void MsgListener::StartListen()
{
    // Here, the compiler give the error:
    // cannot convert 'MsgListener::Listen' from type 'DWORD (MsgListener::)(LPVOID) { aka long unsigned int (MsgListener::)(void*) }' to type 'LPTHREAD_START_ROUTINE { aka long unsigned int (__attribute__((__stdcall__)) *)(void*) }'
    this->_threadHandle = CreateThread(NULL, 0, Listen, 0, &_threadId);
}
void MsgListener::StopListen()
{
    CloseHandle(_threadHandle);
}

The ide I use is Code::Blocks and the compiler is GNU GCC COMPILER.

How to convert the function pointer in a class to the normal function pointer in C? And I really don't know the difference between these two kind of function pointers. Where does each kind of function pointer save in the memory?

0

Share this post


Link to post
Share on other sites

Just about everything you wrote in the post fits in the "probably doing it wrong" category. Singletons should generally be avoided. Threading is not a beginner's topic and is a frequent source of nightmares to engine developers, unguarded shared access is another source of nightmares. Member function pointers require a good understanding of how pointers and classes work in c++. Tagging everything as 'WINAPI' calling convention. And all of that while thinking about conserving memory.

Every one of those has an occasional use, but usually as exceptions that don't fit the usual rules of good design.

What is the actual problem you are trying to solve? What are you trying to build? Just based on the names it looks like something to do with passing messages. There are many excellent existing solutions for that which are much less complex than what you are asking.

Thanks very much. Could you recommand some articles or books about c++ lower mechanisms and theory of c++ class. I always feel confused about some concepts in c++ class.

0

Share this post


Link to post
Share on other sites

How to convert the function pointer in a class to the normal function pointer in C?

You cannot directly specify an instance method as method / callback parameter in Windows API in general, and not in CreateThread in particular. This is because internally instance methods have an additional parameter pointing to the class instance, so the method signature does not match the signature required by CreateThread.
The usual workaround is to have a static method, pass a pointer to the instance as lpParameter, and dispatch the call in the static method to an instance method, using that parameter.

class MsgListener
{
private:
    HANDLE _threadHandle;
    DWORD _threadId;
    static MsgListener *_instance;
    MsgListener();
    DWORD Listen();
    static DWORD WINAPI StaticListen(LPVOID param);
public:
    static MsgListener *GetInstance();
    void StartListen();
    void StopListen();
};

MsgListener *MsgListener::_instance = new MsgListener();
MsgListener::MsgListener() 
{ 
    _threadHandle = 0;
    _threadId = 0;
}
DWORD WINAPI MsgListener::StaticListen(LPVOID param) 
{
	MsgListener* listener = reinterpret_cast<MsgListener*>(param);
	return listener->Listen();
}
DWORD MsgListener::Listen() 
{ 
    // The method to run in a new thread....
	return 0;
}
MsgListener *MsgListener::GetInstance()
{
    if (!_instance)
        _instance = new MsgListener();
    return _instance;
}
void MsgListener::StartListen()
{
    this->_threadHandle = CreateThread(NULL, 0, &MsgListener::StaticListen, this, 0, &_threadId);
}
void MsgListener::StopListen()
{
    CloseHandle(_threadHandle);
}

However, I'd use the threading support in C++11 instead. It is so much easier to specify an arbitrary worker method, even an instance method, using the std::thread ctor:

http://en.cppreference.com/w/cpp/thread/thread/thread

Edited by cdoubleplusgood
1

Share this post


Link to post
Share on other sites

Thanks very much. Could you recommand some articles or books about c++ lower mechanisms and theory of c++ class. I always feel confused about some concepts in c++ class.

Accelerated C++ by Koenig and Moo, and C++ Primer by Lippmann, LaJoie and Moo, are both frequently recommended for beginners in C++.

Even so, what problem are you trying to solve? People can probably point you to excellent resources for whatever specific problem you are looking to solve that are less complex than the path you were traveling.
0

Share this post


Link to post
Share on other sites

 

Thanks very much. Could you recommand some articles or books about c++ lower mechanisms and theory of c++ class. I always feel confused about some concepts in c++ class.

Accelerated C++ by Koenig and Moo, and C++ Primer by Lippmann, LaJoie and Moo, are both frequently recommended for beginners in C++.

Even so, what problem are you trying to solve? People can probably point you to excellent resources for whatever specific problem you are looking to solve that are less complex than the path you were traveling.

 

Thanks, I want to create a new thread to watch the keyboard state and mouse state in DirectInput, I want to use a infinite loop to watch the message by using the GetDevice function in DirectInput. So I want to define a class which is to manage the listen thread.

0

Share this post


Link to post
Share on other sites

 

Could you recommand some articles or books about c++ lower mechanisms and theory of c++ class. I always feel confused about some concepts in c++ class.

http://www.parashift.com/c++-faq/
 
http://www.parashift.com/c++-faq/memfnptr-vs-fnptr.html

 

Thanks, bro~

 

 

How to convert the function pointer in a class to the normal function pointer in C?

You cannot directly specify an instance method as method / callback parameter in Windows API in general, and not in CreateThread in particular. This is because internally instance methods have an additional parameter pointing to the class instance, so the method signature does not match the signature required by CreateThread.
The usual workaround is to have a static method, pass a pointer to the instance as lpParameter, and dispatch the call in the static method to an instance method, using that parameter.

class MsgListener
{
private:
    HANDLE _threadHandle;
    DWORD _threadId;
    static MsgListener *_instance;
    MsgListener();
    DWORD Listen();
    static DWORD WINAPI StaticListen(LPVOID param);
public:
    static MsgListener *GetInstance();
    void StartListen();
    void StopListen();
};

MsgListener *MsgListener::_instance = new MsgListener();
MsgListener::MsgListener() 
{ 
    _threadHandle = 0;
    _threadId = 0;
}
DWORD WINAPI MsgListener::StaticListen(LPVOID param) 
{
	MsgListener* listener = reinterpret_cast<MsgListener*>(param);
	return listener->Listen();
}
DWORD MsgListener::Listen() 
{ 
    // The method to run in a new thread....
	return 0;
}
MsgListener *MsgListener::GetInstance()
{
    if (!_instance)
        _instance = new MsgListener();
    return _instance;
}
void MsgListener::StartListen()
{
    this->_threadHandle = CreateThread(NULL, 0, &MsgListener::StaticListen, this, 0, &_threadId);
}
void MsgListener::StopListen()
{
    CloseHandle(_threadHandle);
}

However, I'd use the threading support in C++11 instead. It is so much easier to specify an arbitrary worker method, even an instance method, using the std::thread ctor:

http://en.cppreference.com/w/cpp/thread/thread/thread

 

Yeah, you are right. When I use a static method, the Singleton is meaningless, so I think I ought to change a design way to watch the device state. Thanks very much, bro.

0

Share this post


Link to post
Share on other sites


When I use a static method, the Singleton is meaningless

You should avoid singletons, but not for this reason. The static thread method is just used to dispatch the call to the instance-specific handler. The "real" handler is not static.

0

Share this post


Link to post
Share on other sites

 


When I use a static method, the Singleton is meaningless

You should avoid singletons, but not for this reason. The static thread method is just used to dispatch the call to the instance-specific handler. The "real" handler is not static.

 

Really thankful to you, I think I know what to do so. Thanks very very much.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0