Advertisement

Loading data

Started by March 15, 2003 02:45 PM
13 comments, last by cavendish 21 years, 11 months ago
quote:
Original post by James Trotter
Take a look at flipcode. They have good tutorials on multithreading.

Yes they do...his example uses CreateThread() which underlying uses AfxBeginThread.
It''s not easy, but it''s cool.

- sighuh?
- sighuh?
I''ll try it right away... Thanks redragon & James Trotter!!

Check out Stargate
--------Whatever
Advertisement
I don''t usually do this, but ohh well.

Here is a thread class I wrote a while back. It should be nice and straight forward. Using it has a catch though you need to be aware of.

I havn''t really tested it so I can''t say it works 100% right.

thread.h

  #pragma once#include <process.h>enum THREAD_PRIORITY{	TP_IDLE,	TP_LOW,	TP_NORMAL,	TP_HIGH,	TP_REALTIME};class CThread{public:	CThread(enum THREAD_PRIORITY priority = TP_NORMAL);	virtual ~CThread(void);	bool threadActive();	void pause();	void resume();	void start();	void changePriority(enum THREAD_PRIORITY priority);protected:	virtual void process()=0;private:	static void entry(void * ptr);	void terminate(void);	void * threadHandle;	bool paused;	bool threadStarted;	int threadPriority;};  


thread.cpp

  #include "stdafx.h"#include "thread.h"CThread::CThread(enum THREAD_PRIORITY priority) : threadHandle(0), paused(0), threadPriority(0), threadStarted(0){	changePriority(priority);}	void CThread::changePriority(enum THREAD_PRIORITY priority){	switch(priority)	{	case TP_IDLE:		threadPriority=-15;		break;	case TP_LOW:		threadPriority=-1;		break;	case TP_NORMAL:		threadPriority=0;		break;	case TP_HIGH:		threadPriority=1;		break;	case TP_REALTIME:		threadPriority=2;		break;	}	if (threadStarted)		SetThreadPriority(threadHandle,threadPriority);}void CThread::start(){	if (threadStarted)		return;	threadStarted=true;	threadHandle=(void*)_beginthread(entry,0,(void*)this);		SetThreadPriority(threadHandle,threadPriority);}CThread::~CThread(void){	terminate();}void CThread::entry(void * ptr){	CThread * thread=(CThread*)ptr;	thread->process();	thread->terminate();}void CThread::terminate(void){	if (threadHandle)		TerminateThread(threadHandle,0);	threadHandle=0;}bool CThread::threadActive(){	return threadHandle!=0;}void CThread::pause(){	if (paused || threadActive())		return;	SuspendThread(threadHandle);	paused=true;}void CThread::resume(){	if (!paused || threadActive())		return;	ResumeThread(threadHandle);	paused=false;}  



to use it you simply make a class that inherits from it and overloads the abstract virtual function ''process''. Then create an instance of the class (preferably using new, which I''ll get onto now).

now. the thing is, the thread can be terminated by both the thread itself and the creation thread. I decided to do this to make it really simple to use. But there is obviously a catch.

What this means is that if you do:


bool load()
{
CThreadedLoader loader(...);

...

return true;
}

the thread object will be destroyed when it exits this function. As such, the thread will be terminated. No matter where it is. This could cause a lot of problems, obviously. So as such, this could be done:

bool load()
{
CThreadedLoader loader(...);

loader.loadFile=.... //setup for loading

loader.start();

...

while (loader.threadActive())
Sleep(100);

return true;
}

that would make the creation thread wait till the new thread is also finished. You could also use new to create the object, and destroy it once it''s no longer active.

Just remember reading and writing data accross a thread is dangerous. it''s ok for simple things like ints and floats, but for, say, stl strings, you could run into problems if one thread is changing the string while another is reading, for example. So you have to do some tricky buffering to avoid memory access violations, etc.


Final note, I put a fair bit of effort into this. so if you do use it some form of acknowlegement would be nice. thats all I ask.

| - Project-X - my mega project.. getting warmer - | - adDeath - an ad blocker I made - | - email me - |
10x RipTorn ... for the code & all ... Threading is okey... As i said i'll test it, but MFC has a class that has the same name as one of my own... I didn't bother to change my class name, so I didn't really test it yet, but I will .. 10x

Check out Stargate

[edited by - cavendish on March 18, 2003 3:17:59 PM]
--------Whatever
I actually tested it today..

small change:


  void CThread::entry(void * ptr){	CThread * thread=(CThread*)ptr;	thread->process();	thread->threadHandle=0;	_endthread();}void CThread::terminate(void){	if (threadHandle)	{		threadHandle=0;		TerminateThread(threadHandle,0);	}}  


| - Project-X - my mega project.. getting warmer - | - adDeath - an ad blocker I made - | - email me - |

This topic is closed to new replies.

Advertisement