multithreaded problems

Started by
5 comments, last by vaneger 20 years, 7 months ago
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.
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.

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
Scott SimontisMy political blog
Of course .H files can include headers, but they shouldn''t.
quote:Original post by Valderman
Of course .H files can include headers, but they shouldn't.

Don't be stupid... why not?

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on September 22, 2003 10:58:01 AM]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
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.
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

This topic is closed to new replies.

Advertisement