Pthread concepts on linux

I have some questions about pthreads on linux:

  • In this case, pthread_t is a data type similar to int and char indicating that we are defining a thread?
  • If so, how much size do you need? 2 bytes or 4 bytes?
  • Does the compiler pthread_t thread1 memory on pthread_t thread1 immediately after this statement, or does it wait for pthread_create() to be called?
  • How to set flow attributes and what is their typical use?
  • Is it possible to pass only one argument in a call to pthread_create() ? If so, how?

I have a lot of things in my opinion. Please feel free to suggest any good sites or documents to read.

+7
source share
5 answers

Answering questions one by one, although not necessarily in the same order:

Is pthread_t a data type similar to int or char , indicating that we are defining a thread? The compiler allocates pthread_t thread1 memory immediately after this sentence or waits until it finds a pthread_create() call

pthread_t is an int like type and is created when it is defined, and not when pthread_create called. In the fragment:

 pthread_t tid; int x = pthread_create (&tid, blah, blah, blah); 

this is the first line that creates the variable, although it does not contain anything useful before returning from pthread_create .

How long is pthread_t , 2 bytes or 4 bytes?

You care about how much space it takes, more than you need, how much space the FILE structure takes. You should just use the structure as intended. If you really want to know, then sizeof is your friend.

Any good info on how to set thread attributes?

If you want to use anything other than the default attributes, you must first create an attribute variable and then pass this to the pthread_create call.

Can we pass only one argument to the pthread_create function of the function? Can't we send 2 or 3 arguments to the pthread_create() function in the called thread?

As long as you are allowed to pass only one additional parameter to the stream, nothing prevents you from making this one parameter a pointer to a structure containing one hundred different things.


If you are looking for information on how to use pthreads, there is a lot of material at the end of the Google search, but I still prefer the dead-tree version:

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

+7
source

how much size is required

pthread_t uses sizeof pthread_t bytes.

and can we only pass one argument to pthread_create for a function of at most one? can we send 2 or 3 arguments to the pthread_create () function in the called thread?

All you need is one argument. All you have is one argument. This is a void * , so you can pass a pointer to whatever you want. For example, a structure containing multiple values.

I have many things, in my opinion, as it offers any good sites or documents to read

Look at the pthread man pages, online or in your selection shell ( man pthread , man pthread_create , etc.). I began to read some basic lecture slides ( continued here).

+1
source

pthread_t can be any number of bytes. It can be char, int, pointer or structure ... But you do not need to know and not care. If you need size for allocation purposes, you use sizeof(pthread_t) . The only type of variable you can assign is another pthread_t .

The compiler may or may not allocate resources related to the thread when you define pthread_t . Again, you don't need to know or care, because you have to call pthread_join (or pthread_detach ) for any thread you create. As long as you follow the rules, the system will ensure that there is no memory leak (or any other resource).

Attributes are admittedly a bit awkward. They are stored in the pthread_attr_t object, which again can be represented as an integer, pointer, or whole structure. You must initialize it with pthread_attr_init and destroy it with pthread_attr_destroy . Between the two, you use the various pthread_attr_... calls to set or clear the 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 these things differently.

LLNL has a decent set of background information .

+1
source

Look in the pthread.h file for more information. On my system, pthread_t is defined as unsigned long int . But I think it depends on the platform, as it is defined in bits/pthreadtype.h .

0
source

You can start with this one to get an idea of ​​pthreads.

-one
source

All Articles