Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create a pthread inside a pthread, which is already created by the main?

Tags:

c++

posix

sockets

Will I run into an error if I create a pthread_create inside a pthread, which is created by the main function. If, I can then what all things I should take care of???

Important....:I am doing a socket programming, where I have opened 5 threads each on separate ports which are listening on ports, when ever I receive a message, I want to create a thread which takes the message and writes into a file using pwrite. So, I have a few questions, please can you help me???

If not then what is another solution of creating a thread inside a thread..??

Or will it give me a segmentation fault???

or will I run into some race conditions....

like image 480
Invictus Avatar asked Sep 29 '11 10:09

Invictus


People also ask

Can a thread create another thread?

Yes. The typical problem, however, is that the work/threads are not constrained. Using the approach you have outlined, it's easy to spawn many threads and have an illogically high number of threads for the work which must be executed on a limited number of cores.

Are pthreads concurrent or parallel?

The Pthreads standard specifies concurrency; it allows parallelism to be at the option of system implementors. As a programmer, all you can do is define those tasks, or threads, that can occur concurrently.

Can a pthread join itself?

A thread cannot join itself because a deadlock would occur and it is detected by the library. However, two threads may try to join each other. They will deadlock, but this situation is not detected by the library. The pthread_join subroutine also allows a thread to return information to another thread.

Are pthreads parallel?

POSIX Threads, commonly known as pthreads, is an execution model that exists independently from a language, as well as a parallel execution model. It allows a program to control multiple different flows of work that overlap in time.


1 Answers

pthread_create creates a new thread. Independently of where it is called. And creating a new thread for a connection when listening on a port is pretty much standard procedure.

like image 97
James Kanze Avatar answered Oct 13 '22 15:10

James Kanze