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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:37:56  来源:igfitidea点击:

pthread concepts in linux

linuxmultithreadingpthreads

提问by karthik

I have some questions about pthreads in linux:

我对 linux 中的 pthreads 有一些疑问:

  1. Is it the case that pthread_tis it a datatype similar to intand charindicating we are defining a thread?
  2. If so, how much size does it take? 2 bytes or 4 bytes?
  3. Does the compiler allocate memory to pthread_t thread1immediately after that statement or does it wait until it a pthread_create()call?
  4. How does one set the thread attributes, and what is their typical use?
  5. Can one only pass more than one argument in the pthread_create()call? If so, how?
  1. pthread_t是一种类似于intchar指示我们正在定义线程的数据类型吗?
  2. 如果是这样,需要多大的尺寸?2 字节还是 4 字节?
  3. 编译器是pthread_t thread1在该语句之后立即分配内存还是等到它pthread_create()调用?
  4. 如何设置线程属性,它们的典型用途是什么?
  5. 只能在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_ta data type similar to intor char, indicating we are defining a thread ? Does the compiler allocate memory to pthread_t thread1immediately after that sentence or does it wait until it finds the pthread_create()call

pthread_t一种数据类型相似int或者char,这表明我们定义一个线程?编译器是pthread_t thread1在该语句之后立即分配内存还是等到它找到pthread_create()调用

pthread_tis a type similar to intand 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_ttake, 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 FILEstructure. You should just use the structure as intended. If you reallywant to know, then sizeofis 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_createcall.

如果你想使用默认属性以外的任何东西,你必须首先创建一个属性变量,然后将它传递给pthread_create调用。

Can we only pass one argument in the pthread_createfunction 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 搜索的末尾有很多东西,但我自己仍然更喜欢死树版本:

PThreads programming book, ISBN 13: 978-1-56592-115-3, ISBN 10: 1-56592-115-1

PThreads 编程书籍,ISBN 13:978-1-56592-115-3,ISBN 10:1-56592-115-1

回答by Matt Ball

how much size does it take

需要多大尺寸

pthread_tuses sizeof pthread_tbytes.

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 pthreadman 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 pthreadman pthread_create,等)。我开始阅读一些基本的讲座幻灯片(这是续集)。

回答by Charles Brunet

Look into pthread.hfile to get more information. On my system, pthread_tis 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_tcould 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_tobject, which again could be represented as an integer, pointer, or entire struct. You have to initialize it with pthread_attr_initand 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_createcalls 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.

不同的实现可以并且将会以不同的方式处理所有这些事情。

LLNL has a decent set of introductory information.

LLNL 有一套不错的介绍性信息