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 02-Apr-2008, 12:43
langers langers is offline
New Member
 
Join Date: Apr 2008
Posts: 1
langers is on a distinguished road

Multithreading, functors, my head hurts... Please help?


We have been given the following assignment to do:

Quote:
Your task is to develop an object-oriented multi-threaded framework. You are to develop a 'thread' class that provide the following services:

1. Creation of suspended threads via a default constructor.
2. Thread suspension and resumption.
3. Thread termination.
4. Cleanup and resource release via the destructor.
5. Thread copying (via deep copy operations).
6. Passing the thread code to the thread via a functor.

To achieve yout objective, it is necessary for you to encapsulate the appropriate MS Windows thread functions.

In addition to the thread class, you are required to demonstrate that your solution works bt providing a test harness that clearly demonstrates the application of all the above features.

Mark allocation:

1. Creation of suspended threads via a default constructor. (2 marks)
2. Thread suspension and resumption. (2 marks)
3. Thread termination. (1 mark)
4. Cleanup and resource release via the destructor. (2 marks)
5. Thread copying (via deep copy operations). (3 marks)
6. Passing the thread code to the thread object via a functor. (4 marks)
7. Test harness, witth sufficient comments to explain it's operation. (6 marks)

So I have started, I can create suspended threads, suspend and resume them, terminate them, I think I have cleaned up my resources as well.

When it comes to using functors, I have no idea what I am doing, I don't understand what they are or how to use them. This is the same with deep copying.

Below is the code I have written so far.

main.cpp
CPP / C++ / C Code:
#include <iostream>
#include <windows.h>
#include "thread.h"

struct outX
{
	void operator()()
	{
		for (;;)
			std::cout << "x";
	}
};

struct outY
{
	void operator()()
	{
		for (;;)
			std::cout << "y";
	}
};

void main()
{
	thread <outX> x(new outX);
	thread <outY> y(new outY);

	Sleep(5000);
}

thread.h
CPP / C++ / C Code:
template <class t>
class thread
{
private:
	DWORD threadID;
	HANDLE hThread;
	t* function;

	static DWORD WINAPI ThreadProc (LPVOID pthis)
	{
		// I think I need to do something with the functor here?
		return 0 ;
	}
	
public:
	thread(t* functor)
	{
		function = functor;
		hThread = CreateThread(NULL, 0, this->ThreadProc, NULL, 0, &threadID);
		SuspendThread(hThread);
	}

	~thread()
	{
		delete function;
	}
	void pause()
	{
		SuspendThread(hThread);
	}

	void resume()
	{
		ResumeThread(hThread);
	}

	void end()
	{
		TerminateThread(hThread, 0);
	}	
};

If anyone can help me out, it would be much appreciated. Especially if they can explain functors and deep copying to me as well.

If you can see anything silly I am doing in the code I have written so far, again, please explain what I have done wrong, and what I should be doing so I know for the future.

Thanks in advance,

Rob
  #2  
Old 03-Apr-2008, 13:55
J-M J-M is offline
New Member
 
Join Date: Apr 2008
Posts: 14
J-M is on a distinguished road

Re: Multithreading, functors, my head hurts... Please help?


Hi Rob.

First that comes to my mind from deep copying is copying an object(class instance) and its member objects. In Java referred to cloning.

Shallow copy copies current object creating new class instance and copies pointers/references to member objects not creating new instances. So the copied object and original object points to the same instances of the member classes. This does not include primitive types as they are in object memory space.

Deep copy copies current object creating new class instance and copies member objects creating new instances of them and giving pointers/references to them to the copied object. So the copied object and original object points to different instances of the member classes.

Hope this helps and please correct me if I'm wrong.

As to funtors(function pointers?) I can't help you.


J-M
  #3  
Old 04-Apr-2008, 12:10
J-M J-M is offline
New Member
 
Join Date: Apr 2008
Posts: 14
J-M is on a distinguished road

Re: Multithreading, functors, my head hurts... Please help?


Found a function pointers tutorial on this forum:
http://www.gidforums.com/t-5466.html
  #4  
Old 05-Apr-2008, 04:10
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough

Re: Multithreading, functors, my head hurts... Please help?


Quote:
Originally Posted by J-M
Found a function pointers tutorial on this forum:
http://www.gidforums.com/t-5466.html

That is a good one.
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

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
Link List In C Peter_APIIT C Programming Language 33 12-Jun-2008 13:33
Str_Misaligned in Double Link List Peter_APIIT C Programming Language 1 29-Feb-2008 20:50
Linked list memory question dabigmooish C++ Forum 3 31-Oct-2006 00:05
search linked list itsmecathys C++ Forum 20 18-Apr-2005 01:34

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

All times are GMT -6. The time now is 12:16.


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