GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Jul-2004, 05:58
YellowFish's Avatar
YellowFish YellowFish is offline
New Member
 
Join Date: Jul 2004
Location: Israel
Posts: 17
YellowFish will become famous soon enough
Post

Multithreading problem


I am running a multi threaded program, and it seems like the threads simply don't run.
Here is a short summary of my code (changed the names of classes and cut off unnecessary code):

these are files from Core.dll project
CPP / C++ / C Code:
// CCore.h
class CCore
{
public:
	CCore();
	~CCore();
// the Init receives pointers to 2 thread-safe queues
	void	Init(CSynQueue* pSendQ, CSynQueue* pRecvQ);
	void	Term();
private:
	HANDLE		m_hSendThreadHandle;
	HANDLE		m_hRecvThreadHandle;
	DWORD		m_pSendThreadID;
	DWORD		m_pRecvThreadID;
	CRITICAL_SECTION	m_CriticalSection;
	bool		m_bSendActive;
	bool		m_bRecvActive;

	void	StartThreads();
	void	EndThreads();
	void	Sender();
	void	Receiver();
	friend DWORD WINAPI	SendThreadFunc(void* Obj);
	friend DWORD WINAPI	RecvThreadFunc(void* Obj);
};


// CCore.cpp

// global functions - these functions are passed to CreateThread as the start
// address where each thread runs

DWORD WINAPI SendThreadFunc(void* Obj)
{
	printf("SENDER THREAD FUNC STARTED\n");
	((Cm104Core*)Obj)->Sender();
	ExitThread(0);
	return 0;
}


DWORD WINAPI RecvThreadFunc(void* Obj)
{
	printf("RECEIVER THREAD FUNC STARTED\n");
	((Cm104Core*)Obj)->Receiver();
	ExitThread(0);
	return 0;
}


void	CCore::Init(CSynQueue* pSendQ, CSynQueue* pRecvQ)
{
// do all sorts of stuff here
// then call:
StartThreads();
}

void CCore::StartThreads()
{
	m_bSendActive = m_bRecvActive = true;
	m_hSendThreadHandle = CreateThread(NULL, 0, SendThreadFunc, this, 0, &m_pSendThreadID);
	m_hRecvThreadHandle = CreateThread(NULL, 0, RecvThreadFunc, this, 0, &m_pRecvThreadID);
}



void CCore::EndThreads()
{
	m_bSendActive = m_bRecvActive = false;
}

void CCore::Sender()
{
while(m_bSendActive )
{
    // do something here
}
}

void CCore::Receiver()
{
while(m_bRecvActive )
{
    // do something here
}
}


in my exe project (lets call it myEXE.exe) i have this file:

CPP / C++ / C Code:
// main.cpp

class CCore* pCore = new CCore;
pCore->Init()
// etc


The problem is that the threads don't seem to run (i never get the line "SENDER THREAD FUNC STARTED" or "RECEIVER THREAD FUNC STARTED").
Any ideas?
  #2  
Old 22-Jul-2004, 06:32
YellowFish's Avatar
YellowFish YellowFish is offline
New Member
 
Join Date: Jul 2004
Location: Israel
Posts: 17
YellowFish will become famous soon enough
it seems like the threads exit right after they start, while debugging i get
Quote:
The thread 0x804 has exited with code 0 (0x0).
The thread 0x934 has exited with code 0 (0x0).
right after i call -
StartThreads();
  #3  
Old 23-Jul-2004, 03:11
RonBurk RonBurk is offline
New Member
 
Join Date: Jun 2004
Posts: 7
RonBurk will become famous soon enough
printf() would not be my first choice of an output debugging function for a thread created by CreateThread(). I would switch to OutputDebugString(). I would also make sure that main thread waits on the threads it creates to complete before exiting.
  #4  
Old 25-Jul-2004, 05:11
YellowFish's Avatar
YellowFish YellowFish is offline
New Member
 
Join Date: Jul 2004
Location: Israel
Posts: 17
YellowFish will become famous soon enough
Quote:
Originally Posted by RonBurk
I would also make sure that main thread waits on the threads it creates to complete before exiting.

That was my problem, Instead of doing something like

CPP / C++ / C Code:
while
{
getchar() ! = 'q';
}

or just a
CPP / C++ / C Code:
Sleep(MY_TIME)
after calling create threads,
I used a breakpoint, apparantly taht wasn't enough.
Thanx
Last edited by dsmith : 25-Jul-2004 at 07:49.
  #5  
Old 25-Jul-2004, 08:00
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
I am using posix threads, but I think they are really similar to the windows threads that you are using. What I have noticed is that if I treat the main function as a thread as well (by calling pthread_exit) in main, it will wait for the other threads to finish.

Example:
CPP / C++ / C Code:
#include "pthread.h"

void* function1()
{
	sleep(1);
	printf("Function 1 called.\n");
	pthread_exit(NULL);
}

void* function2()
{
	sleep(2);
	printf("Function 2 called.\n");
	pthread_exit(NULL);
}


int main(){
	pthread_t	thread1;
	pthread_t	thread2;
	
	pthread_create(&thread1,0,function1,0);
	pthread_create(&thread2,0,function2,0);
	pthread_exit(NULL);
	return 0;
}

This will pause 1 second and print "Function 1 called", pause another second and print "Function 2 called", even though the flow control in main runs through to the end. I would be curious to see if the same is true with the windows threads that you are using.
  #6  
Old 26-Jul-2004, 02:36
YellowFish's Avatar
YellowFish YellowFish is offline
New Member
 
Join Date: Jul 2004
Location: Israel
Posts: 17
YellowFish will become famous soon enough
nice idea dsmith, just tryied it out and your idea works also in windows (using win API function - "ExitThread(0)" ).
chhers
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C I/O problem kelly80 C Programming Language 4 27-Apr-2004 21:15
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 08:53
problem with php5 cgi installation fab13 Apache Web Server Forum 3 19-Nov-2003 10:11
problem to access htdocs folders fraggle two Apache Web Server Forum 5 12-Nov-2003 01:40
unwanted scrollbar problem kelly001 Web Design Forum 3 24-Oct-2003 11:44

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:05.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.