473,480 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Threading classes concept ?

Hi,

I am thinking about using classes to encapsulate threads for my
application.

My requirements are the following:

The thread implementation sould not know what has to be implemented in
the
thread frunction nor it has to define an abstract method to force
implementation
in that. (Then the implementation could not be instantiated)

Also the implementation should not derive from the thread class or
their interface.
(No direct dependencies by linking against the threading library)

That given, I would be able to late bind the real thread
implementation by runtime loading
the instance from any DLL / so module.

Typical samples of a thread class using a static member fuction. But I
prefer to
use a member callback for that. This way I could separate a specific
thread
implementation from the thread base class and also use dynamic loading
of this
class.

In the sample here I have leaved the static member function to be
called at first level.

The trick here I also use on other member callback issues in my
application is a typedef
and store both, the instance to the implementation and the pointer to
the given member
function.

Is there any issue, that let invalidate my concept ?

Thanks for your time !

Lothar

class lb_I_ThreadImplementation;

typedef (lb_I_ThreadImplementation::*lbThreadFunction)();

// My base for all classes (like Mocrosoft COM)
class lb_I_Unknown;

class lb_I_Thread : public lb_I_Unknown {
public:
// The thread implementation instance
lb_I_ThreadImplementation* getThreadImpl() = 0;

// The callback
lbThreadFunction getThreadMemberCallback() = 0;

lbErrCodes setThreadCallback(lb_I_ThreadImplementation* impl,
lbThreadFunction Tfn) = 0;

virtual lbErrCodes create() = 0;

virtual lbErrCodes run() = 0;
virtual lbErrCodes stop() = 0;

virtual lbErrCodes pause() = 0;
virtual lbErrCodes resume() = 0;
};
class lbThreadInternal; // For various operating systems

class lbThread : public lb_I_Thread {
public:
lbThread();
virtual ~lbThread();

lb_I_ThreadImplementation* getThreadImpl();
lbThreadFunction getThreadMemberCallback();
lbErrCodes setThreadCallback(lb_I_ThreadImplementation* impl,
lbThreadFunction Tfn);

lbErrCodes create();

lbErrCodes run();
lbErrCodes stop();

lbErrCodes pause();
lbErrCodes resume();

private:
lbThreadInternal* thread_internal;

lb_I_ThreadImplementation* threadimpl;
lbThreadFunction _tFn;
};

// Platform based implementation
class lbThreadInternal {
public:
lbThreadInternal()
{
lb_hThread = 0;
}

// create a new (suspended) thread (for the given thread object)
lbErrCodes Create(lbThread *thread);

lbErrCodes run();
lbErrCodes stop();

lbErrCodes pause();
lbErrCodes resume();

// thread function
static DWORD WinThreadStart(lbThread *thread);

private:
HANDLE lb_hThread; // handle of the thread
DWORD lb_ThreadId; // thread id
};

DWORD lbThreadInternal::WinThreadStart(lbThread *thread)
{
// store the thread object in the TLS
if ( !::TlsSetValue(s_tlsThisThread, thread) )
{
// Write some useful logmessage
return (DWORD)-1;
}

if (thread == NULL) {
// Write some useful logmessage
return (DWORD)-1;
}

DWORD ret = (thread->getThreadImpl()->*((lbThreadFunction) (thread-
>getThreadMemberCallback()))) ();
delete thread;

return ret;
}

Mar 19 '07 #1
8 1682
Lothar Behrens wrote:
Hi,

I am thinking about using classes to encapsulate threads for my
application.

My requirements are the following:

The thread implementation sould not know what has to be implemented in
the
thread frunction nor it has to define an abstract method to force
implementation
in that. (Then the implementation could not be instantiated)
Why would you want to instantiate the base class?

What's wrong with:

class Thread
{
private:
virtual void thread_func() = 0;
static void start_thread(void* param)
{
static_cast<Thread*>(param)->thread_func();
}
void start()
{
os_specific_thread_starter(&start_thread, this);
}
protected:
Thread() { }
virtual ~Thread() { }
private:
Thread(const Thread&);
Thread& operator=(const Thread&);
};

Then you have the following:

class MyThread : public Thread
{
private:
int n;
public:
MyThread(int n_) : Thread(), n(n_) { }
~MyThread();
private:
void thread_func()
{
std::cout << "Hello from thread " << n << std::endl;
}
};

All you need to do is define thread_func.
Mar 19 '07 #2
red floyd wrote:
Lothar Behrens wrote:
>Hi,

I am thinking about using classes to encapsulate threads for my
application.

My requirements are the following:

The thread implementation sould not know what has to be implemented in
the
thread frunction nor it has to define an abstract method to force
implementation
in that. (Then the implementation could not be instantiated)
I'm not sure exactly what you're trying to say here.
>>

Why would you want to instantiate the base class?

What's wrong with:

class Thread
{
private:
virtual void thread_func() = 0;
static void start_thread(void* param)
{
static_cast<Thread*>(param)->thread_func();
}
void start()
{
os_specific_thread_starter(&start_thread, this);
}
protected:
Thread() { }
virtual ~Thread() { }
private:
Thread(const Thread&);
Thread& operator=(const Thread&);
};

Then you have the following:

class MyThread : public Thread
{
private:
int n;
public:
MyThread(int n_) : Thread(), n(n_) { }
~MyThread();
private:
void thread_func()
{
std::cout << "Hello from thread " << n << std::endl;
}
};

All you need to do is define thread_func.
a) Need synchronization in destructor
b) Must not be copy constructible or assignable
Mar 19 '07 #3
Gianni Mariani wrote:
red floyd wrote:
>>
class Thread
{
private:
virtual void thread_func() = 0;
static void start_thread(void* param)
{
static_cast<Thread*>(param)->thread_func();
}
void start()
{
os_specific_thread_starter(&start_thread, this);
}
protected:
Thread() { }
virtual ~Thread() { }
private:
Thread(const Thread&);
Thread& operator=(const Thread&);
};

Then you have the following:

class MyThread : public Thread
{
private:
int n;
public:
MyThread(int n_) : Thread(), n(n_) { }
~MyThread();
private:
void thread_func()
{
std::cout << "Hello from thread " << n << std::endl;
}
};

All you need to do is define thread_func.

a) Need synchronization in destructor
Left as exercise for the reader. Note that this is really skeleton
code, no error checking etc... It's just "here's the concept" pseudo-code.

b) Must not be copy constructible or assignable
Agreed. I did it in the base, but not the child. Shown as an example.
However, since the base has private unimplemented copy/assignment, the
default copy/assignment for the child should fail, correct?

Mar 19 '07 #4
Lothar Behrens wrote:
Hi,

I am thinking about using classes to encapsulate threads for my
application.
Try boost::thread
Mar 19 '07 #5
Lothar Behrens a écrit :
Hi,

I am thinking about using classes to encapsulate threads for my
application.

My requirements are the following:

The thread implementation sould not know what has to be implemented in
the
thread frunction nor it has to define an abstract method to force
implementation
in that. (Then the implementation could not be instantiated)

Also the implementation should not derive from the thread class or
their interface.
(No direct dependencies by linking against the threading library)

That given, I would be able to late bind the real thread
implementation by runtime loading
the instance from any DLL / so module.
My understanding is that you want to define an abstract factory for your
threading object (you might add condition and mutexes).
>
Typical samples of a thread class using a static member fuction. But I
prefer to
use a member callback for that. This way I could separate a specific
thread
implementation from the thread base class and also use dynamic loading
of this
class.
The two are equivalent, you may as well start with a static function and
then make wrappers to call member functions.
>
In the sample here I have leaved the static member function to be
called at first level.

The trick here I also use on other member callback issues in my
application is a typedef
and store both, the instance to the implementation and the pointer to
the given member
function.

Is there any issue, that let invalidate my concept ?
[snip code]
lbErrCodes run();
lbErrCodes stop();
lbErrCodes pause();
lbErrCodes resume();
You may not be able to implement all functions on all plateform (there
is not always a way to stop a thread).

Michael
Mar 19 '07 #6
red floyd wrote:
Gianni Mariani wrote:
....
>b) Must not be copy constructible or assignable

Agreed. I did it in the base, but not the child. Shown as an example.
However, since the base has private unimplemented copy/assignment, the
default copy/assignment for the child should fail, correct?
I missed that - you're correct.

Mar 19 '07 #7

"Lothar Behrens" <lo************@lollisoft.dewrote in message
news:11**********************@p15g2000hsd.googlegr oups.com...
Hi,

I am thinking about using classes to encapsulate threads for my
application.
[...]

Here is a very brief outline for a possible CO++ threading scheme...

http://groups.google.com/group/comp....9c36e0e1123ece

;^)
Mar 19 '07 #8
On 19 Mar, 21:37, "Chris Thomasson" <cris...@comcast.netwrote:
"Lothar Behrens" <lothar.behr...@lollisoft.dewrote in message

news:11**********************@p15g2000hsd.googlegr oups.com...Hi,
I am thinking about using classes to encapsulate threads for my
application.

[...]

Here is a very brief outline for a possible CO++ threading scheme...

http://groups.google.com/group/comp....msg/679c36e0e1...

;^)
FWIW to provide some contrast, there is a fair amount of activity
related to standardising concurrency in the next generation of C++.

http://www.open-std.org/jtc1/sc22/wg...007/n2169.html

(Head down to the "Active topics in Evolution" section on the page,
where most of the proposals seem to be).

regards
Andy Little


Mar 20 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
13150
by: willie | last post by:
Hi, I'm writing a program which requires the use of three serial ports and one parallel port. My application has a scanning devices on each port, which I can access fine with pyserial. ...
77
5206
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
11
3791
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
6
1196
by: Yuk Tang | last post by:
Is there a basic guide to threading online? Something that not only talks about the syntax, but also the concept and ideas related to it. I'm still used to the school of single-thread programming,...
9
2162
by: akrapus | last post by:
Hi, I am trying to understand how to use threading in Python. I get threading as a concept, but not the implementation. In order to start threading, do you call it as a separate function,...
7
286
by: Bruce W. Darby | last post by:
I've been doing some reading on threading, using the BackgroundWorker, but I'm getting in over my head a bit. I've run through the tutorial at MSDN and, while it has some good information, it is a...
2
1650
by: danielhamd | last post by:
I am somewhat new to the concept of remoting in c#. Here is my scenario, if anyone could give me some tips I would really appreciate it: I have a server program running that updates a database...
6
4005
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
126
6593
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard:...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7081
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6737
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6921
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5336
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.