问题描述
我对linux中的pthread有一些疑问:
I have some questions about pthreads in linux:
- 是否是
pthread_t
是类似于int
和char
的数据类型,表明我们正在定义线程? - 如果是这样,需要多少尺寸? 2个字节还是4个字节?
- 编译器是在该语句之后立即将内存分配给
pthread_t thread1
还是等待直到它调用pthread_create()
? - 如何设置线程属性,它们的典型用途是什么?
- 在
pthread_create()
调用中只能传递一个以上的参数吗?如果可以,怎么办?
- 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?
我脑海中有很多这样的事情.也请随时建议任何好的站点或文档以供阅读.
I have lots of things on my mind like this. Please also feel free to suggest any good sites or documents to read.
推荐答案
一个问题一个接一个地回答,尽管顺序不一定相同:
Answering the questions one by one, though not necessarily in the same order:
pthread_t
是类似于int
或char
的数据类型,指示我们正在定义线程吗?编译器是在该语句之后立即将内存分配给pthread_t thread1
还是等待直到找到pthread_create()
调用
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
类似的类型,它是在定义时创建的,而不是在调用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 tid;
int x = pthread_create (&tid, blah, blah, blah);
是创建变量的 first 行,尽管直到从pthread_create
返回之前它没有任何用处.
it's the first line that creates the variable, although it doesn't hold anything useful until the return from pthread_create
.
pthread_t
的大小是2个字节还是4个字节?
How much size does a pthread_t
take, 2 bytes or 4 bytes?
您无需关心它占用了多少空间,也不必关心FILE
结构占用了多少空间.您应该只使用预期的结构.如果您真的想知道,那么sizeof
是您的朋友.
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 really want to know, then sizeof
is your friend.
关于如何设置线程属性的所有好信息吗?
如果要使用默认属性以外的任何内容,则必须先创建一个属性变量,然后将其传递给pthread_create
调用.
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
函数中的一个参数传递给该函数吗?我们不能在pthread_create()
函数中将2或3个参数发送给被调用的线程吗?
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?
虽然只允许向线程传递一个额外的参数 ,但没有什么可以阻止您将此一个参数设置为指向包含一百种不同事物的结构的指针.
While you're only allowed to pass one extra parameter to the thread , there's nothing stopping you from making this one parameter a pointer to a structure holding a hundred different things.
如果您正在寻找有关如何使用pthread的信息,那么Google搜索的末尾有很多东西,但我本人还是更喜欢死树版本:
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:
这篇关于Linux中的pthread概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!