Jump to content

Multithreading Doubt

- - - - -

  • Please log in to reply
6 replies to this topic

#1
Daya

Daya

    Newbie

  • Members
  • Pip
  • 3 posts
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 :)

#2
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
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.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#3
Firebird_38

Firebird_38

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
Mutithreading is only useful (I think...:)) when
  • 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
Any other situation is better solved using a single thread (I think :)). Threads have overhead. Sometimes it may be "easier" to program a solution using threads, but firing off hundreds of threads is just not smart and wastes valuable processor time on switching between threads.

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.

Posted Image


#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Daya

Daya

    Newbie

  • Members
  • Pip
  • 3 posts
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 :)

#6
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
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
Daya

Daya

    Newbie

  • Members
  • Pip
  • 3 posts
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 :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users