Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pthread concepts in linux

I have some questions about pthreads in linux:

  1. Is it the case that pthread_t is it a datatype similar to int and char indicating 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 thread1 immediately 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?

I have lots of things on my mind like this. Please also feel free to suggest any good sites or documents to read.

like image 213
karthik Avatar asked Jun 21 '11 03:06

karthik


2 Answers

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:

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

like image 156
paxdiablo Avatar answered Sep 28 '22 07:09

paxdiablo


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).

like image 22
Matt Ball Avatar answered Sep 28 '22 07:09

Matt Ball