0

I have a question in regards to creating threads.

Specifically I want to know the difference between looping through thread[i] and not looping but recalling pthread_create

For Example

A. Initializes 5 threads

for(i=0,i<5;i++){
pthread_create(&t[i],NULL,&routine,NULL);
}

B. Incoming clients connecting to a server

while(true){

client_connects_to_server = accept(sock, (struct sockaddr *)&server,  
                   (socklen_t*)&server_len)

pthread_create(&t,NULL,&routine,NULL); //no iteration 
}

Is the proper method of creating threads for incoming clients, to keep track of the connections already made, maybe something like this ?

pthread_create(&t[connections_made+1],&routine,NULL)

My concern is not being able to handle concurrent pthreads if option B is terminating threads or "re-writing" client connections.

Here is an example where no iteration is done https://gist.github.com/oleksiiBobko/43d33b3c25c03bcc9b2b

Why is this correct ?

Jay
  • 111
  • 10
  • Would you please clarify what difference you see between these alternatives that you think could be significant? Certainly iteration `vs` no iteration does not itself make any difference, except in how many threads are created. – John Bollinger Mar 17 '21 at 03:39
  • @john So if 5 clients connect to the server at the exact sametime. I want 5 threads to be created. Does option B allow for this to take place ? – Jay Mar 17 '21 at 03:42
  • Yes. Why wouldn't it? – John Bollinger Mar 17 '21 at 03:44
  • So you are saying with Option B these 5 threads exist at the same time. They don't rewrite each other. And Queuing of these 5 threads is possible using semaphore to protect critical sections. – Jay Mar 17 '21 at 03:48
  • Yes. I still don't follow what makes you doubt this. – John Bollinger Mar 17 '21 at 03:49
  • well I guess the fact that I've seen examples of threads being created in for loops...led me to believe threads can only live concurrently if they were created through iteration – Jay Mar 17 '21 at 03:54
  • Both your examples are iterative. But indeed, no, iteration has nothing in particular to do with thread lifetime. – John Bollinger Mar 17 '21 at 03:56
  • Thanks John that's a crucial thing to understand. Apparently it's a popular question...will probably delete this one https://stackoverflow.com/questions/35061854/creating-multiple-threads-in-c – Jay Mar 17 '21 at 04:02
  • BTW, NULL as the last parameter to pthread_create() does not give me a warm, fuzzy feeling:(( Somewhat like Arnie, you'll be back:(( – Martin James Mar 17 '21 at 12:02

1 Answers1

0

Contrary to your apparent assertion, both your examples call pthread_create() inside loops. In example A, it is a for loop that will iterate a known number of times, whereas in example B, it is a while loop that will iterate an unbounded number of times. I guess the known number vs unbounded number is what you mean by "static" and "dynamic", but that is not a conventional usage of those terms.

In any event, pthread_create() does what it is documented to do. Like any other function, it does not know anything about the context from which it is called other than the arguments passed to it. It can fail, but that's not influenced by looping by the caller, at least not directly. When pthread_create() succeeds, it creates and starts a new thread, which runs until the top-level call to its thread function returns, pthread_exit() is called by thread, the thread is canceled, or the process terminates.

The main significant difference between your two examples is that A keeps all the thread IDs by recording them in different elements of an array, whereas B overwrites the previous thread ID each time it creates a new thread. But the thread IDs are not the threads themselves. If you lose the ID of a thread then you can no longer join it, among other things, but that doesn't affect the thread's operation, including its interactions with memory, with files, or with synchronization objects such as semaphores. In this regard, example B is more suited for a thread function that will detach the thread in which it is called, so that the joining issue is moot. Example A's careful preservation of all the thread IDs would pointless for threads that detach themselves, but necessary if the threads need to be joined later.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157