MultiThreading using Pthreads

Thread Tools Search this Thread
Top Forums Programming MultiThreading using Pthreads
# 1  
Old 12-14-2009
MultiThreading using Pthreads

i have multiple pthread_create calls like this:

pthread_create(...., ThreadFunc1,.....);
pthread_create(...., ThreadFunc2,.....);

which i am using to create multiple threads.All the "ThreadFunc<i>" functions are actually calling same function "Receive" of a class using same object of that class like this:

obj.Receive(<socket 1>); //receive data from socket 1

obj.Receive(<socket 2>); //receive data from socket 2

1.Will all the threads have their own copies of function "Receive", in order to provide concurrent receiving from all the receive sockets in application.

2. How is this approach different from:

obj.Receive(<from all sockets one by one>);

where the same function polls all the receive sockets one by one to see for any data on them.
# 2  
Old 12-14-2009
You almost certainly need synchronization somewhere inside ::Receive() to prevent race conditions -- two threads blindly overwriting the same variable, unaware of the other changing it while they were asleep -- but nothing prevents this except whatever synchronization you do yourself. How efficient this is under high load depends on how much waiting the threads have to do for each other to avoid race conditions. If Receive() does a lot of processing before actually altering or using the object members in any way, most of that could happen concurrently, but if all it does is update the object, almost nothing should happen concurrently, making the threads mostly pointless.

Polling isn't a good idea in either case though, given a better alternative: Have you tried select()? It can wait for activity on an entire set of file descriptors without polling. Once activity happens, you can hand the file descriptor it reports off to a thread for processing if advantageous, or just feed it to Receive() if not.

Last edited by Corona688; 12-14-2009 at 01:01 PM..
# 3  
Old 12-15-2009
"If Receive() does a lot of processing before actually altering or using the object members in any way, most of that could happen concurrently, but if all it does is update the object, almost nothing should happen concurrently, making the threads mostly pointless."

Can u please elaborate this point...
# 4  
Old 12-15-2009
I'd just be restating what I said already: Things that don't contend for the same resource can be done concurrently. Using local variables isn't contention, but global variables would, static variables would, and class members would. Modifying any of those would be contention, which would need a mutex to guarantee that only one thread at a time uses it -- that is, reads OR writes it -- at once.

Ergo, if modifying class members is nearly all that Receive() does, there's not a lot of point in multithreading it since you'll have to force them to use the object one-at-a-time anyway to avoid race conditions. What else did you want to know?

Last edited by Corona688; 12-15-2009 at 05:19 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Help with multithreading

I take this question of the The Linux Programming Interface: A Linux and Unix System Programming page 652 exercise 30.1 I want someone to explain the under line statement because it sounds complex to me couldn't understand anything 30-1 Modify the program (thread_incr.c) so that each loop in... (3 Replies)
Discussion started by: fwrlfo
3 Replies

2. Shell Programming and Scripting

Multithreading program

Hi I need to insert 1million records into MySQL database, but it is taking lot of time as there is no bulk insert support. I want to spawn 10 processes which will insert 100k records each parallely. Can somebody help me with a example program to execute this task through shell scripting. (5 Replies)
Discussion started by: sach_roger
5 Replies

3. UNIX for Advanced & Expert Users

multithreading in UNIX

Hi, Can you please give me a suitable reference to learn multithreading programming in C in UNIX? Thanks (3 Replies)
Discussion started by: naan
3 Replies

4. Programming

multithreading on OSX

Hi all, I have a query about multithreading. What I would like to do is, at the start of my main update() function, start a couple of threads in parallel, once they are all complete carry on with my main update function. void update() { thread1->update(); // fluid solver ... (3 Replies)
Discussion started by: memoid
3 Replies

5. HP-UX


Hi! I'm linking my hpux code using -lpthread (gcc), yet it references libpthread_tr.1, the debug version of the pthread lib. How do I force it to use pthreads? Thanks, :) (3 Replies)
Discussion started by: zackz
3 Replies

6. Programming


I have created a thread program, it is attached. My problem is that I need to loop this program multiple times, and basically reset everything including the threads created previously. I try to loop the program, the first run is fine, as always, but the second run of the program, the initialize... (0 Replies)
Discussion started by: justgotthis
0 Replies

7. Programming


howcome that pthtreads spawn 2 extra processes? I'm kind of new with pthreads but fork() did not act like this. Anyone who can give me a technical explanation of what happends with mother / daughter processes? Best regards Esaia. (2 Replies)
Discussion started by: Esaia
2 Replies

8. Programming


Does any one no of some good web site which will explain about how to program using pthreads in a UNIX enviroment? (6 Replies)
Discussion started by: fishman2001
6 Replies

9. UNIX for Advanced & Expert Users


Can anyone explain me how to use pthread_key_create() , pthread_setspecific(), pthread_getspecific() and pthread_key_delete () routines in pthreads. Kindly state by an example. (3 Replies)
Discussion started by: S.P.Prasad
3 Replies
Login or Register to Ask a Question

Featured Tech Videos