Sponsored Content
Top Forums Programming MultiThreading using Pthreads Post 302380162 by Corona688 on Monday 14th of December 2009 11:53:01 AM
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..
 

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

PThreads

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

2. Programming

pthreads

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

3. Programming

pthreads

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

4. Programming

PThreads

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

5. HP-UX

pthreads

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

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

7. 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

8. 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

9. 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
PTHREAD_CREATE(3)					   BSD Library Functions Manual 					 PTHREAD_CREATE(3)

NAME
pthread_create -- create a new thread SYNOPSIS
#include <pthread.h> int pthread_create(pthread_t *restrict thread, const pthread_attr_t *restrict attr, void *(*start_routine)(void *), void *restrict arg); DESCRIPTION
The pthread_create() function is used to create a new thread, with attributes specified by attr, within a process. If attr is NULL, the default attributes are used. If the attributes specified by attr are modified later, the thread's attributes are not affected. Upon suc- cessful completion, pthread_create() will store the ID of the created thread in the location specified by thread. Upon its creation, the thread executes start_routine, with arg as its sole argument. If start_routine returns, the effect is as if there was an implicit call to pthread_exit(), using the return value of start_routine as the exit status. Note that the thread in which main() was originally invoked differs from this. When it returns from main(), the effect is as if there was an implicit call to exit(), using the return value of main() as the exit status. Upon thread exit the storage for the thread must be reclaimed by another thread via a call to pthread_join(). Alternatively, pthread_detach() may be called on the thread to indicate that the system may automatically reclaim the thread storage upon exit. The pthread_attr_setdetachstate() function may be used on the attr argument passed to pthread_create() in order to achieve the same effect as a call to pthread_detach() on the newly created thread. The signal state of the new thread is initialized as: o The signal mask is inherited from the creating thread. o The set of signals pending for the new thread is empty. RETURN VALUES
If successful, the pthread_create() function will return zero. Otherwise, an error number will be returned to indicate the error. ERRORS
pthread_create() will fail if: [EAGAIN] The system lacked the necessary resources to create another thread, or the system-imposed limit on the total number of threads in a process [PTHREAD_THREADS_MAX] would be exceeded. [EINVAL] The value specified by attr is invalid. SEE ALSO
fork(2), pthread_cleanup_pop(3), pthread_cleanup_push(3), pthread_detach(3), pthread_exit(3), pthread_join(3) STANDARDS
pthread_create() conforms to ISO/IEC 9945-1:1996 (``POSIX.1''). BSD
April 4, 1996 BSD
All times are GMT -4. The time now is 01:25 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy