LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-25-2004, 08:59 AM   #1
socket9001
LQ Newbie
 
Registered: Dec 2003
Location: Israel
Distribution: Slackware
Posts: 21

Rep: Reputation: 15
threads......


im working on an application that handles incoming connections....
each connection is handled with a thread.. the max number of threads is 100

imy code looks like this:

Code:
int threads=0, max_threads=100;

main()
{
         while( SOMETHING )
         {
                  if(threads < max_threads)
                  {
                           do_stuff();
                           threads++;
                           pthread_create(&tid, NULL, &my_thread_function, NULL);
                  }       
         }
}

my_thread_function()
{
         do_more_stuff();
         ...
         ...
         ...
         threads--;
}
im having problems figuring out what am i suppose to do with the thread id var?
an array ?
how can i tell when an tid value is ready for use again ?

thanks in advance for your help....
 
Old 04-25-2004, 11:47 PM   #2
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
why do you have to worry about the thread ids leave that to the theads lib ...instead you should just worry abouyt the number of threads that you have spawned so that you may stop creating any more threads once you limit has been reached...

the moment you see that you have exceeded hundred ..kill the thread you might want to kill by using pthread_cancel/pthread_kill or pthread_exit(from inside the thread)...

and you might want to store the thread ids in an array so that you may want to kill or cancel...
 
Old 04-26-2004, 05:27 AM   #3
socket9001
LQ Newbie
 
Registered: Dec 2003
Location: Israel
Distribution: Slackware
Posts: 21

Original Poster
Rep: Reputation: 15
so i can use the same pthread_t variable for all the threads i create ?

pthread_t tid;
for(i=0; i<100; i++)
pthread_create(&tid, NULL, &thread, NULL);

is these legal ?
 
Old 04-26-2004, 06:49 AM   #4
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
while this might work..but you'd not be able to wait for those threads(and thus control those threads, as you would like to)...as you are overwriting the same memory over and over again...thus leaving you with just the tid of the last thread created, and not all the threads..
that is why i was asking you to create and array of threead ids...

those ids would be used to wait for a thread in pthread_join...for sending signals to a particular thread, etc
 
Old 04-26-2004, 09:05 AM   #5
socket9001
LQ Newbie
 
Registered: Dec 2003
Location: Israel
Distribution: Slackware
Posts: 21

Original Poster
Rep: Reputation: 15
ok, thanks alot
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java threads listed using kill -3 does not contain all threads found using ps -auxww coneheed Programming 2 11-14-2005 08:57 AM
How often do threads go ot? lectraplayer General 5 09-26-2005 09:26 PM
c++ threads siemens Programming 4 07-10-2005 11:21 AM
Java Threads vs Native Threads rjmendez Programming 0 08-16-2004 05:58 AM
Useful threads....... chingasman Linux - General 1 02-21-2003 09:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration