对象和线程之间的联系是什么? [英] What's the connection between objects and threads?

查看:72
本文介绍了对象和线程之间的联系是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我必须编写一个多线程程序。我决定采用OO

方法。我有想法在一个名为Threadable的混合类中包装所有线程函数

。然后当一个对象在它自己的线程中运行

时,它应该实现这个混合类。这个

听起来似乎是合情合理的设计决定吗?


我很惊讶C ++没有这样的功能,比如说它的

STL。在C ++中没有线程/对象关系导致我

相信我的想法不是很好。


我将非常感谢您的见解。谢谢

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

推荐答案

darren写道:
darren wrote:




我必须编写一个多线程程序。我决定采用OO

方法。我有想法在一个名为Threadable的混合类中包装所有线程函数

。然后当一个对象在它自己的线程中运行

时,它应该实现这个混合类。这个

听起来似乎是合情合理的设计决定吗?


我很惊讶C ++没有这样的功能,比如说它的

STL。 C ++中缺少线程/对象关系导致我相信我的想法不是很好。
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.



C ++标准的下一个版本确实有线程支持。


下一版本中的线程概念将可能与您描述的内容有关,但有一些较新的概念 - 一个称为未来。


同时,有一些替代库 - 提升工具

许多标准的新功能已经和其他类似的

commonc ++和奥地利c ++也有

C ++。


在我的大多数新项目中,我使用的线程池与线程非常不同


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.


darren< mi ****** @ gmail.comwrote in news:7083eb1a-4dbd-4b94-866b-
fd ********** @ c19g2000prf.googlegroups.com
darren <mi******@gmail.comwrote in news:7083eb1a-4dbd-4b94-866b-
fd**********@c19g2000prf.googlegroups.com:

您好


我必须编写一个多线程程序。我决定采用OO

方法。我有想法在一个名为Threadable的混合类中包装所有线程函数

。然后当一个对象在它自己的线程中运行

时,它应该实现这个混合类。这个

听起来似乎是合情合理的设计决定吗?


我很惊讶C ++没有这样的功能,比如说它的

STL。在C ++中没有线程/对象关系导致我

相信我的想法不是很好。


我将非常感谢您的见解。谢谢
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



当前用于C ++线程化的半官方解决方案是boost :: thread

库。从我所听到的C ++ 0x标准也将基于

那个。


在boost :: thread中有一个仿函数对象,将其复制到

启动的线程然后执行。每个仿函数对象都有自己的线程

函数。我认为任何Threadable基类或接口都没有意义。


原则上,没有对象在任何线程中运行。只有对象''方法

可能会在某个线程或其他线程中运行,具体取决于它们调用的是哪个线程。
。至少有两种有用的对象类型:


- 单线程对象,其方法可能无法并行运行$ / b $ b不同的线程。通过一次只能看到一个线程,

对象的指针可以最好地确保这一点。将对象

传递给另一个线程是可能的,确保在当前所有者线程中没有指向对象或其内部的指针。

- 多线程对象,可以随时从任何

线程调用所有公共方法。通常这种对象在访问他们的私人数据时执行内部

锁定。


hth

Paavo

The current semi-official solution for threading in C++ is boost::thread
library. From what I''ve heard the C++0x standard will also be based on
that.

In boost::thread one has a functor object, which is copied over to the
started thread and then executed. Each functor object has its own thread
function. I see no point for any Threadable base class or "interface".

In principle, no object does run in any thread. Only the objects'' methods
might run in some thread or another, depending on which thread they are
called. There are at least two useful types of objects:

- single-threaded objects, whose methods may not run in parallel by
different threads. This is best ensured by that the pointer(s) to the
object are visible only to a single thread at a time; passing the object
to another thread is possible by ensuring there are no pointers left to
the object or its inners in the current owner thread.

- multi-threaded objects, whose all public methods can be called from any
thread at any time. Typically objects of this kind perform internal
locking when accessing their private data.

hth
Paavo


5月17日,10:20 * pm,darren< minof ... @ gmail.comwrote:


什么对象和线程之间的联系是什么?


好​​问题。在C ++中,对象和

线程之间没有连接,因为C ++中没有线程。


C ++没有进程或线程的概念

在语言层面的计算。另一方面,C ++在语言层面确实具有对象的概念。它是一个面向对象的面向对象的编程语言,但不是一个面向对象的并发的

。坏消息是它也不会是一个。


好​​消息是你可以将计算线程引入C

++程序通过一些库。即将推出的C ++ 0x甚至会包含这样一个库。


因此,您可以使用任何并发库将线程混合到一个
C ++中面向对象的程序。计算的对象和线程通常是彼此正交的。
。对象是(被动)数据

结构,另一方面,计算线程相当于你的程序中有自己的程序计数器的
活动。


您可以将它们分开,就像在第一个基于类的
并发编程语言Concurrent Pascal中所做的那样,或者您可以将它们组合在一起

就像在早期语言提案中所做的那样,

称为分布式进程(DP)。
On May 17, 10:20*pm, darren <minof...@gmail.comwrote:

"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).

我有想法结束所有线程函数都是名为Threadable的混合类中的

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?



看起来你的方法是关于对象是数据的方法

结构(被动元素)和计算线程(有效)

元素)彼此正交。在这种情况下,你可以将它们分开,并且不要混合它们,这不是一个好主意。

虽然,如果你必须使用任何较低级别的库(如作为Pthread),

构建自己的更高级别的抽象是个好主意,我想b $ b想想。


在此

对象之间的区别非常重要,这些对象只能从一个

计算的线程中使用,也可以在要共享的对象之间使用

许多线程。必须保护共享对象,以便线程只能以互斥的方式访问它们(参见

监视器概念)。


最诚挚的问候,

Szabolcs

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


这篇关于对象和线程之间的联系是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆