The question is simple. Lets say in my main() I call a system call to create a thread and to it I pass "main" as the a function pointer. What is the behavior? Theoretically the stack should overflow once the maximum number of concurrent threads are reached and the call should fail. But I have theory, I contest that since "main" is called every time, a new stack will be initialised and this program will run infinitely. Please validate this theory :)
6 replies to this topic
#1
Posted 06 June 2010 - 05:46 AM
|
|
|
#2
Posted 06 June 2010 - 07:16 AM
Your theory is wrong -- the operating system has a limit on the number of threads at any one time. This is a sytem limit, not a process limit.
Hereis a rather old thread about a similar topic you may find interesting.
Hereis a rather old thread about a similar topic you may find interesting.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.
#3
Posted 06 June 2010 - 11:52 PM
Mutithreading is only useful (I think...:)) when
That said, there are many situations when you do want threads. Apache is such a situation. Please note that threads sit waiting on work 99% of the time, however. That's the first bullet above.
- One thread is doing nothing, while another does something and/or a thread needs to react asap to an asynchronous event such as network activity.
- A thread needs to stay responsive to user input
- You want to utilize all cores or processors
That said, there are many situations when you do want threads. Apache is such a situation. Please note that threads sit waiting on work 99% of the time, however. That's the first bullet above.
#4
Posted 07 June 2010 - 04:49 AM
Threads are pretty common in servers. You have a main thread that listens for requests, and it creates a thread to handle each connection. That way, you can have multiple connections handled at once, without having to wait for the connection ahead of the current one to finish.
#5
Posted 07 June 2010 - 09:05 PM
Thank you all for sharing your knowledge.
@Ancient Dragon, I am not able to see the link that you have posted.
Continuing the discussion further, I am creating threads by using standard pthread library's funtion called the pthread_create(). My question is, since I am passing main as the function pointer to the pthread_create(), does it create another process beacuse of the keyword "main" or does it transparently submit the function to the operating system to schedule the newly created thread?
PS: I am asking such questions for the sake of understanding and not for realtime applications :)
@Ancient Dragon, I am not able to see the link that you have posted.
Continuing the discussion further, I am creating threads by using standard pthread library's funtion called the pthread_create(). My question is, since I am passing main as the function pointer to the pthread_create(), does it create another process beacuse of the keyword "main" or does it transparently submit the function to the operating system to schedule the newly created thread?
PS: I am asking such questions for the sake of understanding and not for realtime applications :)
#6
Posted 08 June 2010 - 01:53 AM
pthread_create() creates threads, not processes.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.
#7
Posted 08 June 2010 - 02:32 AM
Yes.
I verified that after posting this question. This is what I did.
# include "stdio.h" 03:26:44 PM
# include "pthread.h"
int main()
{
pthread_t tid1;
int ret;
static int i;
ret = pthread_create(&tid1,NULL,main,NULL);
if (i%100==0)
printf("\nthread_id%d for index %d\n",tid1,i);
i++;
for (;;);
}
It turned out the static variable is updated in each call to the thread. So this is confirmed. Thank you all :)
I verified that after posting this question. This is what I did.
# include "stdio.h" 03:26:44 PM
# include "pthread.h"
int main()
{
pthread_t tid1;
int ret;
static int i;
ret = pthread_create(&tid1,NULL,main,NULL);
if (i%100==0)
printf("\nthread_id%d for index %d\n",tid1,i);
i++;
for (;;);
}
It turned out the static variable is updated in each call to the thread. So this is confirmed. Thank you all :)
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top










