Pthread Concepts in Linux

pthread concepts in linux

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

Understanding Pthreads

I get the same results of the book with linux that contains the libc libuClibc-0.9.30.1.so (1).

root@OpenWrt:~# ./test
main thread pid is 1151
child thread pid is 1153

and I tried to run this program with a linux that contains the libc from ubuntu libc6 (2)

$ ./test
main thread pid is 2609
child thread pid is 2609

The libc (1) use linuxthreads implementation of pthread

And the libc (2) use NPTL ("Native posix thread library") implementation of pthread

According to the linuxthreads FAQ (in J.3 answer):

each thread is really a distinct process with a distinct PID, and
signals sent to the PID of a thread can only be handled by that thread

So in the old libc which use linuxthreads implementation, each thread has its distinct PID

In the new libc version which use NPTL implementation, all threads has the same PID of the main process.

The NPTL was developed by redhat team. and according to the redhat NPTL document: One of the problems which are solved in the NPTL implementation is:

(Chapter: Problems with the Existing Implementation, page5)

Each thread having a different process ID causes compatibility
problems with other POSIX thread implementations. This is in part a
moot point since signals can'tbe used very well but is still
noticeable


And that explain your issue.

You are using the new libc version that contains the NPTL ("Native posix thread library") implementation of pthread

And the Book use an old version of libc that contains linuxthreads implementation of pthread

What's a Pthread?

Threads are a generic concept. Wikipedia defines it as:

In computer science, a thread of execution is the smallest sequence of programmed instructions that can be managed independently by an operating system scheduler. A thread is a light-weight process.

Pthreads or POSIX threads are one implementation of that concept used with C program on Unix. Most modern languages have their own implementation of threads. From that web page:

Pthreads are defined as a set of C language programming types and procedure calls, implemented with a pthread.h header/include file and a thread library - though this library may be part of another library, such as libc, in some implementations.

Pthreads as a standard solution for simultaneous console input in UNIX?

You can take a look at the select(2) system call and read about multiplexing IO.

When to use pthread_exit() and when to use pthread_join() in Linux?

As explained in the openpub documentations,

pthread_exit() will exit the thread that calls it.

In your case since the main calls it, main thread will terminate whereas your spawned threads will continue to execute. This is mostly used in cases where the
main thread is only required to spawn threads and leave the threads to do their job

pthread_join
will suspend execution of the thread that has called it unless the target thread terminates

This is useful in cases when you want to wait for thread/s to terminate before further
processing in main thread.

learning threads on linux

std::thread is boost::thread accepted into C++11 with some extras. My understanding is that if boost::thread gets replaced in code with std::thread it should still compile and work.

boost::thread is based on pthreads design, providing thin C++ wrappers over thread, mutex and condition variables. Thread cancellation though was left outside the scope of C++11, since there was no agreement how it should work in C++.

So, by learning pthreads you also learn std::thread concepts. std::thread adds mostly syntax sugar and convenience functions on top of pthreads C API.

With regards to WaitForMultipleObjects(), neither pthreads nor std::thread provide anything similar to its bWaitAll=FALSE mode, however, it's routinely simulated using pipes and select() on UNIX, or more modern eventfd() and epoll() on Linux. bWaitAll=TRUE mode can be simulated by waiting on all tasks in turn, since it doesn't proceed until all objects are ready anyway.



Related Topics



Leave a reply



Submit