I have some questions about pthreads in linux:
- Is it the case that
pthread_tis it a datatype similar tointandcharindicating 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 thread1immediately 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:
Is
pthread_ta data type similar tointorchar, indicating we are defining a thread ? Does the compiler allocate memory topthread_t thread1immediately after that sentence or does it wait until it finds thepthread_create()callpthread_tis a type similar tointand it’s created when you define it, not when you callpthread_create. In the snippet: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_ttake, 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
FILEstructure. You should just use the structure as intended. If you really want to know, thensizeofis 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_createcall.Can we only pass one argument in the
pthread_createfunction to the function? Can’t we send 2 or 3 arguments in thepthread_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: