linux中的pthread概念
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6420090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pthread concepts in linux
提问by karthik
I have some questions about pthreads in linux:
我对 linux 中的 pthreads 有一些疑问:
- Is it the case that
pthread_t
is it a datatype similar toint
andchar
indicating we are defining a thread? - If so, how much size does it take? 2 bytes or 4 bytes?
- Does the compiler allocate memory to
pthread_t thread1
immediately after that statement or does it wait until it apthread_create()
call? - How does one set the thread attributes, and what is their typical use?
- Can one only pass more than one argument in the
pthread_create()
call? If so, how?
- 它
pthread_t
是一种类似于int
并char
指示我们正在定义线程的数据类型吗? - 如果是这样,需要多大的尺寸?2 字节还是 4 字节?
- 编译器是
pthread_t thread1
在该语句之后立即分配内存还是等到它pthread_create()
调用? - 如何设置线程属性,它们的典型用途是什么?
- 只能在
pthread_create()
调用中传递多个参数吗?如果是这样,如何?
I have lots of things on my mind like this. Please also feel free to suggest any good sites or documents to read.
我脑子里有很多这样的事情。也请随时推荐任何好的网站或文档来阅读。
采纳答案by paxdiablo
Answering the questions one by one, though not necessarily in the same order:
一一回答问题,但不一定按相同的顺序:
Is pthread_t
a data type similar to int
or char
, indicating we are defining a thread ? Does the compiler allocate memory to pthread_t thread1
immediately after that sentence or does it wait until it finds the pthread_create()
call
是pthread_t
一种数据类型相似int
或者char
,这表明我们定义一个线程?编译器是pthread_t thread1
在该语句之后立即分配内存还是等到它找到pthread_create()
调用
pthread_t
is a type similar to int
and it's created when you define it, not when you call pthread_create
. In the snippet:
pthread_t
是一种类似于的类型int
,它是在您定义它时创建的,而不是在您调用pthread_create
. 在片段中:
pthread_t tid;
int x = pthread_create (&tid, blah, blah, blah);
it's the firstline that creates the variable, although it doesn't hold anything useful until the return from pthread_create
.
它是创建变量的第一行,尽管在从pthread_create
.
How much size does a pthread_t
take, 2 bytes or 4 bytes?
需要多少大小pthread_t
,2 字节或 4 字节?
You shouldn't care how much space it takes, any more than you should care how much space is taken by a FILE
structure. You should just use the structure as intended. If you reallywant to know, then sizeof
is your friend.
你不应该关心它需要多少空间,就像你应该关心一个FILE
结构占用了多少空间一样。您应该按预期使用该结构。如果你真的想知道,那sizeof
就是你的朋友。
Any good information about how to set the thread attributes?
关于如何设置线程属性的任何好信息?
If you want to use anything other than default attributes, you have to create an attributes variable first and then pass that to the pthread_create
call.
如果你想使用默认属性以外的任何东西,你必须首先创建一个属性变量,然后将它传递给pthread_create
调用。
Can we only pass one argument in the pthread_create
function to the function? Can't we send 2 or 3 arguments in the pthread_create()
function to the called thread?
我们可以只将函数中的一个参数传递pthread_create
给函数吗?我们不能将函数中的 2 或 3 个参数发送pthread_create()
到被调用线程吗?
While you're only allowed to pass oneextra parameter to the thread , there's nothing stopping you from making this one parameter a pointer to a structure holding a hundred different things.
虽然您只允许向线程传递一个额外的参数,但没有什么可以阻止您将这个参数设为指向包含一百种不同内容的结构的指针。
If you're looking for information on how to use pthreads, there's plenty of stuff at the end of a Google search but I still prefer the dead-tree version myself:
如果您正在寻找有关如何使用 pthreads 的信息,在 Google 搜索的末尾有很多东西,但我自己仍然更喜欢死树版本:
回答by Matt Ball
how much size does it take
需要多大尺寸
pthread_t
uses sizeof pthread_t
bytes.
pthread_t
使用sizeof pthread_t
字节。
and we can only pass one argument in the pthread_create to the function not more than one? cant we send 2 or 3 arguments in the pthread_create() function to the called thread?
并且我们只能将 pthread_create 中的一个参数传递给不超过一个的函数?我们不能在 pthread_create() 函数中向被调用线程发送 2 或 3 个参数吗?
All you need is one argument. All you get is one argument. It's a void *
so you can pass a pointer to whatever you want.Such as a structure containing multiple values.
你所需要的只是一个论据。你得到的只是一个论据。这是一个void *
让您可以将指针传递给任何你想要的。例如包含多个值的结构。
i have lots of things on my mind like this suggest any good sites or documents to read
我有很多像这样的事情,建议任何好的网站或文档阅读
Have a look at the pthread
man pages, onlineor in your shell of choice (man pthread
, man pthread_create
, etc.). I started out reading some basic lecture slides(here's the sequel).
有一个看看pthread
手册页,在网上或在您选择的外壳(man pthread
,man pthread_create
,等)。我开始阅读一些基本的讲座幻灯片(这是续集)。
回答by Charles Brunet
Look into pthread.h
file to get more information. On my system, pthread_t
is defined as an unsigned long int
. But I guess this is platform dependent, since it is defined into bits/pthreadtype.h
.
查看pthread.h
文件以获取更多信息。在我的系统上,pthread_t
被定义为unsigned long int
. 但我想这取决于平台,因为它被定义为bits/pthreadtype.h
.
回答by Nemo
pthread_t
could be anynumber of bytes. It could be a char, an int, a pointer, or a struct... But you neither need to know nor to care. If you need the size for allocation purposes, you use sizeof(pthread_t)
. The only type of variable you can assign it to is another pthread_t
.
pthread_t
可以是任意数量的字节。它可以是一个字符、一个整数、一个指针或一个结构......但你既不需要知道也不需要关心。如果出于分配目的需要大小,请使用sizeof(pthread_t)
. 您可以将其分配给的唯一变量类型是 another pthread_t
。
The compiler may or may not allocate the resources associated with the thread when you define a pthread_t
. Again, you do not need to know nor to care, because you are required to call pthread_join
(or pthread_detach
) on any thread you create. As long as you follow the rules, the system will make sure it does not leak memory (or any other resource).
当您定义pthread_t
. 同样,您不需要知道或关心,因为您需要在您创建的任何线程上调用pthread_join
(或pthread_detach
)。只要你遵守规则,系统就会确保它不会泄漏内存(或任何其他资源)。
Attributes are admittedly a bit clumsy. They are held in an pthread_attr_t
object, which again could be represented as an integer, pointer, or entire struct. You have to initialize it with pthread_attr_init
and destroy it with pthread_attr_destroy
. Between those two, you use various pthread_attr_...
calls to set or clear attributes, and then you can pass it as part of one or more pthread_create
calls to set the attributes of the new threads.
不可否认,属性有点笨拙。它们保存在一个pthread_attr_t
对象中,该对象又可以表示为整数、指针或整个结构。你必须用 初始化它pthread_attr_init
并用 销毁它pthread_attr_destroy
。在这两者之间,您可以使用各种pthread_attr_...
调用来设置或清除属性,然后您可以将其作为一个或多个pthread_create
调用的一部分传递来设置新线程的属性。
Different implementations can and will handle all of these things differently.
不同的实现可以并且将会以不同的方式处理所有这些事情。