Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

vaneger

multithreaded problems

This topic is 5389 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

im not sure how to use this properly any one got some advice?
#pragma once
#define WINDOWS_LEAN_AND_MEAN
#include <windows.h>

class cThread
{
protected:
  HANDLE  d_threadHandle;
  DWORD   d_threadID;
  bool    d_bIsRunning;

public:
  cThread();
  virtual ~cThread();

  void Begin();
  void End();
  bool IsRunning();

  virtual DWORD ThreadProc();
};
#include"cThread.h"
cThread::cThread()
{
  d_threadID = 0;
  d_threadHandle = NULL;
  d_bIsRunning = false;
}
cThread::~cThread()
{
  End();
}
void cThread::Begin()
{
#if defined( _WIN32 ) && defined( _MT )
  if( d_threadHandle )
    End();  // just to be safe.


  // Start the thread.

  d_threadHandle = CreateThread( NULL,
                                 0,
                                 (LPTHREAD_START_ROUTINE)cThreadProc,
                                 this,
                                 0,
                                 (LPDWORD)&d_threadID );
  if( d_threadHandle == NULL )
  {
    // Arrooga! Dive, dive!  And deal with the error, too!

  }
  d_bIsRunning = true;
#endif
}
DWORD cThread::ThreadProc()
{
  return 0;
}
static DWORD WINAPI cThreadProc( cThread *pThis )
{
  return pThis->ThreadProc();
}

void cThread::End()
{
#if defined( _WIN32 ) && defined( _MT )
  if( d_threadHandle != NULL )
  {
    d_bIsRunning = false;
    WaitForSingleObject( d_threadHandle, INFINITE );
    CloseHandle( d_threadHandle );
    d_threadHandle = NULL;
  }
#endif
}
bool cThread::IsRunning()
{
	return(d_bIsRunning);
}
#include<iostream.h>
#include"mThread.h"
mThread::mThread()
{
	mData = 0;
}
void mThread::add(int amount = 1)
{
	mData += amount;
}
mThread::~mThread()
{
	End();
}
DWORD mThread::ThreadProc()
{
  this->add();
  cout<<mData<<endl;
  return 0;
}
#include"cThread.h"
class mThread : public cThread
{
public:
	mThread();
	~mThread();
	DWORD ThreadProc();
	void add(int amount);
	int mData;
};
#include"mThread.h"
#include<iostream.h>
void main()
{
	mThread *x = new mThread;
	cThread *q = new mThread;
	cout<<"hello"<<endl;
	x->Begin();
	cout<<x->mData<<endl;
	q->Begin();
}

im not sure how to get it working so that begin actually starts the thread, which should increment mData then output it then end. the cThread class is originally from FlipCode so it should work i just dont knwo hwo to get it working.

Share this post


Link to post
Share on other sites
Advertisement
I didnt follow your code through all the way and i am no c++ expert, but it seems that you didnt include the cThread.h in you main cpp file... you are declaring an object reference and allocation new memory to it, but the object reference is of an object that doesn''t exist as far as that code is concerned - i.e. no cThread class.

Share this post


Link to post
Share on other sites
quote:
Original post by kalash
I didnt follow your code through all the way and i am no c++ expert, but it seems that you didnt include the cThread.h in you main cpp file... you are declaring an object reference and allocation new memory to it, but the object reference is of an object that doesn''t exist as far as that code is concerned - i.e. no cThread class.




I believe .h files cannot include headers,. so it is being ignored. You should use sentry statements at the top of your main file to make sure that everything is correct. For example:

#ifndef __CTHREAD_H__
#define __CTHREAD_H__
#endif


Scott Simontis
e-mail:ageofscott@comcast.net
AIM:ssimontis

Share this post


Link to post
Share on other sites
quote:
Original post by vaneger
im not sure how to use this properly any one got some advice?
im not sure how to get it working so that begin actually starts the thread, which should increment mData then output it then end.
the cThread class is originally from FlipCode so it should work i just dont knwo hwo to get it working.

Are you linking against multithreaded libs? Note that the MT code only gets compiled into your program if _WIN32 and _MT are defined, so make sure you choose the correct project settings.
quote:
Original post by kalash
you are declaring an object reference and allocation new memory to it, but the object reference is of an object that doesn''t exist as far as that code is concerned

What on earth gives you that idea? If the compiler could not see the class definition at point of instantiation, it would not know how much memory it needed to allocate, and would complain quite loudly.

Share this post


Link to post
Share on other sites
ok the dude at flipcode messed up a bit when he wrote cThread. when he called create thread he didnt pass the proper cThreadProc address of cThreadProc(this). it works now though

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!