Thread: Threads in C++

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    4

    Threads in C++

    Hi,

    In the constructor of my class Scene I call a thread "CreateThread" that not recognized the attributes of my class... By any chance, is it a property on the use of threads???

    Code:
    Scene::Scene()
    {
    	HANDLE hThreadTest;
    	unsigned long iID; 
                    spnavi = new Device();
    	spnavi -> initDevice();
    	hThreadTest = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadTest,NULL,0,&iID);
    }
    
    DWORD WINAPI Scene::ThreadTest (void) {
    	while (true) {
    		spnavi -> loop();
    	}
    }
    The message error is:
    1>.\Class Scene.cpp(33) : error C2227: left of '->loop' must point to class/struct/union/generic type.

    Does anyone have any idea?

    Thanks.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    What is spnavi?

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    4
    Is an attribute of the class (Scene) that represents an interaction physical device. Is an object of the other class (Device).

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    How is it defined?

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You cannot pass a member function as the thread start routine. It must be a static or non-member function.

    Better, just use Boost.Thread for this stuff. Then you can use Boost.Bind to pass a member function.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    4
    Quote Originally Posted by medievalelks View Post
    How is it defined?
    The class Device is defined how:

    Code:
    #include "dispositivo.h"
    
    using namespace std;
    
    Device::Device() {
    ...
    }
    
    void Device::initDevice() {
    ...
    }
    
    void Device::loop() {
    ... //get coordinates of device...
    }

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    hThreadTest = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadTest,NULL,0,&iID);
    Do NOT cast the address of a function unless you really, really, really, REALLY know what you're doing (hint: you don't).
    If you remove it, the compiler will complain. As it should, because you cannot pass a pointer to a class function where a pointer to a global or static function is expected.
    This is a good thing to remember. Casting the address will mask your error for the compiler.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Also, a thread function should take a void* pointer parameter, not void.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Another subtle and easily identified bug if you would just not cast the function address.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    4
    Hi,

    Finally, I compiled and executed the program. The function is:

    Code:
    HANDLE WINAPI Scene::ThreadTest (Device* spnavi) {
    	while (true) {
    		spnavi -> loop();
    	}
    }
    The function above gets the coordinates (translation and rotation) of the user interaction device, however, seems to me that the thread is centralizing the execution of the application, as the main window of 3D application does not appear! I need to read the coordinates of the device in parallel to execution of 3D application...

    By definition, this could not happen... anyone have any idea?

    Thanks.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, first, your loop is endless, which is bad. You should loop on a variable so that you can break the loop later.
    Second, it will eat up 100% CPU (or 50% on dual core) unless you have some sleeps somewhere (most likely), so make sure you do that. The best bet is just make it wait until it has work to do, otherwise it's just waste of CPU cycles which games seem to be really good at.

    And if you don't mind, what's the code for creating that thread?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    Well, first, your loop is endless, which is bad. You should loop on a variable so that you can break the loop later.
    It could be broken by an exception.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Right, but I don't see any catch handlers.
    So either throw/catch or flag.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    Right, but I don't see any catch handlers.
    So either throw/catch or flag.
    This is really too speculative. But the lack of a catch would be exactly how the loop gets broken -- the exception propagates past it.

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Into the threading infrastructure? Not a good idea.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-17-2008, 11:28 AM
  2. Yet another n00b in pthreads ...
    By dimis in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2008, 12:43 AM
  3. Classes and Threads
    By Halloko in forum Windows Programming
    Replies: 9
    Last Post: 10-23-2005, 05:27 AM
  4. problem with win32 threads
    By pdmarshall in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2004, 02:39 PM
  5. Block and wake up certain threads
    By Spark in forum C Programming
    Replies: 9
    Last Post: 06-01-2002, 03:39 AM