I have some questions about pthreads in linux:
- 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 开发者_C百科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:
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
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);
it's the first line that creates the variable, although it doesn't hold anything useful until the return from pthread_create
.
How much size does a pthread_t
take, 2 bytes or 4 bytes?
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.
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.
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.
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:
how much size does it take
pthread_t
uses sizeof pthread_t
bytes.
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?
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.
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, online or in your shell of choice (man pthread
, man pthread_create
, etc.). I started out reading some basic lecture slides (here's the sequel).
pthread_t
could be any number 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
.
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).
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.
Different implementations can and will handle all of these things differently.
LLNL has a decent set of introductory information.
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
.
精彩评论