I have some questions about pthreads in linux:
pthread_t
is it a datatype similar to int
and char
indicating we are defining a thread?pthread_t thread1
immediately after that statement or does it wait until it a pthread_create()
call? 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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With