Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What's the connection between objects and threads?

Reply
Thread Tools

What's the connection between objects and threads?

 
 
darren
Guest
Posts: n/a
 
      05-17-2008
Hi

I have to write a multi-threaded program. I decided to take an OO
approach to it. I had the idea to wrap up all of the thread functions
in a mix-in class called Threadable. Then when an object should run
in its own thread, it should implement this mix-in class. Does this
sound like plausible design decision?

I'm surprised that C++ doesn't have such functionality, say in its
STL. This absence of a thread/object relationship in C++ leads me to
believe that my idea isn't a very good one.

I would appreciate your insights. thanks
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      05-17-2008
darren wrote:
> Hi
>
> I have to write a multi-threaded program. I decided to take an OO
> approach to it. I had the idea to wrap up all of the thread functions
> in a mix-in class called Threadable. Then when an object should run
> in its own thread, it should implement this mix-in class. Does this
> sound like plausible design decision?
>
> I'm surprised that C++ doesn't have such functionality, say in its
> STL. This absence of a thread/object relationship in C++ leads me to
> believe that my idea isn't a very good one.


The next revision of the C++ standard does in fact have thread support.

The threading concepts in the next revision will probably work with what
you describe but there is a few newer concepts - one is called a "future".

In the meantime, there are some alternative libraries - boost implements
many of the standard's new features already as well as other like
commonc++ and Austria c++ also have cross platform threading support for
C++.

In most of my newer projects, I use thread pools which is very different
to threads.
 
Reply With Quote
 
 
 
 
Szabolcs Ferenczi
Guest
Posts: n/a
 
      05-17-2008
On May 17, 10:20*pm, darren <(E-Mail Removed)> wrote:

"What's the connection between objects and threads?"

Good question. In C++ there is no connection between objects and
threads since there are no threads in C++.

C++ does not have the notion of the process or the thread of
computation at the language level. On the other hand, C++ does have
the notion of the object at the language level. It is an object-
oriented programming language but not a concurrent object-oriented
one. The bad news is that it is not going to be one either.

The good news is that you can introduce threads of computation into a C
++ program via some libraries. The coming C++0x would even include
such a library, though.

So, you can take any concurrency libraries to mix threads into an
object-oriented program in C++. Objects and threads of computation are
usually orthogonal to each other. Objects are (passive) data
structures and, on the other hand, threads of computation are rather
activities with their own program counters in your program.

You can keep them separate as was done in the first class-based
concurrent programming language, Concurrent Pascal, or you can combine
them together as was done, for instance, in an early language proposal
called Distributed Processes (DP).

> I had the idea to wrap up all of the thread functions
> in a mix-in class called Threadable."


What do you mean by "thread functions"?

> Does this
> sound like plausible design decision?


It looks like you have the approach in mind where the objects are data
structures (passive elements) and the threads of computation (active
elements) are orthogonal to each other. In this case you could keep
them separate and do not intermix them, it would not be a good idea.
Although, if you must use any lower level library (such as Pthread),
it is a good idea to build your own higher level abstractions, I
think.

In this case it is very important to make a difference between the
objects which are meant to be used exclusively from one thread of
computation only and between the objects which are to be shared among
many threads. The shared objects must be protected so that they could
be accessed by the threads in a mutually exclusive way only (see the
Monitor concept).

Best Regards,
Szabolcs
 
Reply With Quote
 
darren
Guest
Posts: n/a
 
      05-18-2008
Thanks for the responses everybody.

I have actually heard of Boost. I saw it in another post on this user
group about threads. My dilemma is that for this project, we were
specifically told that we must use the pthread library and our
deliverable must only be source code and a Makefile.

Doest Boost use pthread library calls in its implementation, or are
they up to something else? Maybe I'll email my prof and ask if using
Boost is permitted.

As for my current idea:

I planned to write a class called myThreads which would wrap up calls
to the pthread library, like pthread_create. Then anytime i need a
thread I'll instantiate one of these objects. I was thinking that in
one of these 'myThread" constructors, a new thread would be created
and ran. Again, i'm new to thread programming, so i dont know if this
will work or not.

there was also mention about threadpools. Our instructor said this may
be a good way to implement our project. How are they significantly
different than concepts of thread programming (as somebody here
mentioned they were).

thanks again.
 
Reply With Quote
 
darren
Guest
Posts: n/a
 
      05-18-2008
On May 17, 8:35 pm, "Chris Thomasson" <(E-Mail Removed)> wrote:
> "darren" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
> > Thanks for the responses everybody.

>
> > I have actually heard of Boost. I saw it in another post on this user
> > group about threads. My dilemma is that for this project, we were
> > specifically told that we must use the pthread library and our
> > deliverable must only be source code and a Makefile.

>
> > Doest Boost use pthread library calls in its implementation, or are
> > they up to something else? Maybe I'll email my prof and ask if using
> > Boost is permitted.

>
> > As for my current idea:

>
> > I planned to write a class called myThreads which would wrap up calls
> > to the pthread library, like pthread_create. Then anytime i need a
> > thread I'll instantiate one of these objects. I was thinking that in
> > one of these 'myThread" constructors, a new thread would be created
> > and ran. Again, i'm new to thread programming, so i dont know if this
> > will work or not.

>
> [...]
>
> You don't generally want to create threads in constructors because of a
> possible race-condition. Think of starting a thread in an object's
> constructor which starts to run and operate on it _before_ its ctor has
> finished... Anyway:
>
> http://groups.google.com/group/comp..../browse_frm/th...
> (please read all!)
>
> Here is some very simple source-code that might help you:
> __________________________________________________ __________________
>
> /* VERY Simple Thread Library Code
> __________________________________________________ ____________*/
> #include <pthread.h>
>
> class thread_base;
> extern "C" void* thread_base_entry(void*);
> static void thread_create(thread_base* const);
> static void thread_join(thread_base* const);
>
> class thread_base {
> pthread_t m_id;
> friend void* thread_base_entry(void*);
> friend void thread_create(thread_base* const);
> friend void thread_join(thread_base* const);
> virtual void on_thread_entry() = 0;
> public:
> virtual ~thread_base() throw() = 0;
>
> };
>
> thread_base::~thread_base() throw() {}
>
> void* thread_base_entry(void* state) {
> reinterpret_cast<thread_base*>(state)->on_thread_entry();
> return 0;
>
> }
>
> void thread_create(thread_base* const _this) {
> int const status = pthread_create(&_this->m_id, NULL,
> thread_base_entry, _this);
> if (status) {
> throw int(status);
> }
>
> }
>
> void thread_join(thread_base* const _this) {
> int const status = pthread_join(_this->m_id, NULL);
> if (status) {
> throw int(status);
> }
>
> }
>
> /* Very Simple Application Code
> __________________________________________________ ____________*/
> #include <cstdio>
>
> class my_thread : public thread_base {
> public:
> my_thread() {
> std:rintf("(%p)-my_thread::my_thread()\n",
> reinterpret_cast<void*>(this));
> }
>
> ~my_thread() throw() {
> std:rintf("(%p)-my_thread::~my_thread() throw()\n",
> reinterpret_cast<void*>(this));
> }
>
> private:
> void on_thread_entry() {
> std:rintf("void (%p)-my_thread:n_thread_entry()\n",
> reinterpret_cast<void*>(this));
> }
>
> };
>
> #define RUN_DEPTH() 10
> #define THREAD_DEPTH() 32
> int main() {
> int runs = 0;
> for (; runs < RUN_DEPTH(); ++runs) {
> int i = 0;
> my_thread threads[THREAD_DEPTH()];
>
> try {
> for (; i < THREAD_DEPTH(); ++i) {
> thread_create(&threads[i]);
> }
> } catch (int const& e) {
> std:rintf("thread_create throws status %d!\n", e);
> }
>
> for (--i; i > -1; --i) {
> try {
> thread_join(&threads[i]);
> } catch (int const& e) {
> std:rintf("thread_join throws status %d!\n", e);
> }
> }
> }
>
> /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~*/
> std:uts("\n\n___________________________________ _________\n\
> Press <ENTER> to exit...");
> std::getchar();
> return 0;
>
> }
>
> __________________________________________________ __________________
>
> Any questions?


hi Chris. Thanks for the reply and the sample code. I'll have to
spend some time understanding it. For starters why do you have this
ection:

> class thread_base;
> extern "C" void* thread_base_entry(void*);
> static void thread_create(thread_base* const);
> static void thread_join(thread_base* const);


I've never seen code like this. are the parameters taking pointers to
a class??? it seems like thread_create is declared twoice, once as
friend and once not...

I've also never seen extern C before, i'll have to look up what that
is.

thanks again.
 
Reply With Quote
 
ManicQin
Guest
Posts: n/a
 
      05-18-2008
On May 18, 3:24 am, darren <(E-Mail Removed)> wrote:
> Thanks for the responses everybody.
>
> I have actually heard of Boost. I saw it in another post on this user
> group about threads. My dilemma is that for this project, we were
> specifically told that we must use the pthread library and our
> deliverable must only be source code and a Makefile.
>
> Doest Boost use pthread library calls in its implementation, or are
> they up to something else? Maybe I'll email my prof and ask if using
> Boost is permitted.
>
> As for my current idea:
>
> I planned to write a class called myThreads which would wrap up calls
> to the pthread library, like pthread_create. Then anytime i need a
> thread I'll instantiate one of these objects. I was thinking that in
> one of these 'myThread" constructors, a new thread would be created
> and ran. Again, i'm new to thread programming, so i dont know if this
> will work or not.
>
> there was also mention about threadpools. Our instructor said this may
> be a good way to implement our project. How are they significantly
> different than concepts of thread programming (as somebody here
> mentioned they were).
>
> thanks again.


At the company that I'm working we work the way you suggested
(which could be imply that YOU DONT WANT TO USE IT! )
but it works fine and most of the times it's transparent.

My problem with thread pools (and I'll be happy if someone contradicts
me)
is that until .Net did'nt introduce thread pools I have'nt heared
about it
and it makes me worried that maybe it's a bit platform specific.
(But if you dont care than it's great method to ease your work)

If you could shed some light on your project maybe people could
give a more precise counsel on the MT method you should use...
 
Reply With Quote
 
darren
Guest
Posts: n/a
 
      05-18-2008
On May 18, 1:13 am, ManicQin <(E-Mail Removed)> wrote:
> On May 18, 3:24 am, darren <(E-Mail Removed)> wrote:
>
>
>
> > Thanks for the responses everybody.

>
> > I have actually heard of Boost. I saw it in another post on this user
> > group about threads. My dilemma is that for this project, we were
> > specifically told that we must use the pthread library and our
> > deliverable must only be source code and a Makefile.

>
> > Doest Boost use pthread library calls in its implementation, or are
> > they up to something else? Maybe I'll email my prof and ask if using
> > Boost is permitted.

>
> > As for my current idea:

>
> > I planned to write a class called myThreads which would wrap up calls
> > to the pthread library, like pthread_create. Then anytime i need a
> > thread I'll instantiate one of these objects. I was thinking that in
> > one of these 'myThread" constructors, a new thread would be created
> > and ran. Again, i'm new to thread programming, so i dont know if this
> > will work or not.

>
> > there was also mention about threadpools. Our instructor said this may
> > be a good way to implement our project. How are they significantly
> > different than concepts of thread programming (as somebody here
> > mentioned they were).

>
> > thanks again.

>
> At the company that I'm working we work the way you suggested
> (which could be imply that YOU DONT WANT TO USE IT! )
> but it works fine and most of the times it's transparent.
>
> My problem with thread pools (and I'll be happy if someone contradicts
> me)
> is that until .Net did'nt introduce thread pools I have'nt heared
> about it
> and it makes me worried that maybe it's a bit platform specific.
> (But if you dont care than it's great method to ease your work)
>
> If you could shed some light on your project maybe people could
> give a more precise counsel on the MT method you should use...


Its a multithreaded web server using pthreads and the socket api (hey
everybody else in my class looking for ideas ).

Here's my basic algorithm:
1. main() makes a singleton ServerKernal object to control program
flow
2. ServerKernal listens for requests on a specified port. this is done
by creating a thread that loops and continuously accepts connections.
3. A successful accepted connection is put into a queue (from the STL
library).
4. Meanwhile, back at the ranch, another thread is created and running
that continuously checks for entries into the queue.
5. if an entry is in there, a new thread is spawned to handle the
request. That thread then dies.

What do you think of this algorithm?

A couple of things i'm thinking about:
1. As per the reason i created this post, I was thinking about how
objects related to threads. At first I was thinking that an object
should create a thread. this has been advised as bad (thanks for the
advice!). Now I'm thinking that instead, threads should create
objects, and destroy them before the thread ends. for example, when a
request is in the queue, a thread is created to handle it. Within this
new thread, a RequestHandler object is instantiated. It's methods are
used to process the request. The object is then destroyed. Finally,
the thread ends.

2 I was thinking about thread pools. Maybe there could be 10 threads
set up, waiting for requests. I dont know much about thread pools yet
though. It may not be appropriate. Or, maybe the threads could be
sleeping, then awoken by signaling when a request enters the queue.

 
Reply With Quote
 
Szabolcs Ferenczi
Guest
Posts: n/a
 
      05-18-2008
On May 18, 11:43*am, darren <(E-Mail Removed)> wrote:

> At first I was thinking that an object
> should create a thread. *this has been advised as bad (thanks for the
> advice!). *


That is not a bad idea at all that an object creates a thread in the
constructor. Do not be mistaken.

There are programming models where there are objects which are active
entities. As I earlier mentioned one early proposal of this kind is
the Distributed Processes programming concept. This means that it
depends on discipline only. You can build an abstraction for yourself
where the object starts its dedicated thread as the last step in the
constructor and joins the low level thread in the destructor. That is
not a bad idea at all.

That means that you can use configurator blocks in your program where
you declare so-called thread objects and shared objects together.

{
Buffer b;
Producer p(b);
Consumer c(b);
}

The block realises a parallel block or fork-join block as they call it
in a trendy terminology.

Best Regards,
Szabolcs
 
Reply With Quote
 
Szabolcs Ferenczi
Guest
Posts: n/a
 
      05-18-2008
On May 18, 11:43*am, darren <(E-Mail Removed)> wrote:

> Here's my basic algorithm:
> 1. main() makes a singleton ServerKernal object to control program
> flow


> What do you think of this algorithm?


Try to avoid using singletons (if you refer to the singleton pattern).
It is not a good idea even in non-MT programs. You can always avoid
using singletons by disciplined programming. In this case you do not
need singleton either.

Best Regards,
Szabolcs
 
Reply With Quote
 
Szabolcs Ferenczi
Guest
Posts: n/a
 
      05-18-2008
On May 18, 2:24*am, darren <(E-Mail Removed)> wrote:

> Doest Boost use pthread library calls in its implementation, or are
> they up to something else? *


As far as I know, Boost is built on Pthread library on the Unix
platform. In principle it could be implemented on any other low level
libraries as well on Unix too. That is the benefit of higher level
structures that they hide low level details.

Best Regards,
Szabolcs
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing data between objects and calling all objects of a class in turn ghoetker Python 1 08-25-2010 03:18 AM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
Exange of informations and objects between PERL and JAVA Lionel Java 1 08-23-2004 11:34 AM
Exange of informations and objects between PERL and JAVA Lionel Perl 0 08-23-2004 10:48 AM



Advertisments