![]() |
| | #1 |
| Registered User Join Date: Sep 2008
Posts: 44
| I'm very new to pthreads and I'm not sure exactly how I would init this data structure. I have the following data structure: Code: /* thread work */
typedef struct thread_work {
FILE *fp;
int fd;
struct thread_work *next;
} thread_work_t;
/* a thread pool */
typedef struct tpool {
/* pool attributes */
int num_threads;
/* pool state */
pthread_t *threads;
/* receiver mutexes */
pthread_mutex_t read_ipc_lock; /* need to read from ipc channel */
pthread_mutex_t next_entry_lock; /* need to update what the next entry to write is */
pthread_mutex_t write_file_lock; /* need to write to the file when it's your turn */
pthread_mutex_t last_entry_lock; /* need to identify the last entry */
/* cond vars */
pthread_cond_t next_entry_check; /* cond var for waiting for your turn */
} tpool_t;
/* one each for child and parent */
tpool_t *tp; /* thread pool */
extern int thread_pool_init( int poolSize );
extern int run_pool_threads( FILE *fp, int fd, void *(*threadfn)(void *));
and I'm going to use this function to initiate the data structure: Code:
/**********************************************************************
Function : thread_pool_init
Description : Pthreads version to initialize pool of size poolSize
Inputs : poolSize - number of threads in the thread pool
Outputs : 0 if successful, -1 if failure
***********************************************************************/
int thread_pool_init( int poolSize )
{
/* initialize thread pool data structure */
/* everything is OK */
return 0;
}
Something like: Code: thread_work_t *t_work;
if (( t_work = (thread_work_t *) malloc( sizeof(thread_work_t) ) ) == NULL) {
|
| mr_coffee is offline | |
| | #2 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| So how are thread_work_t and tpool_t related? I mean, you have this, but the comment and declaration make no sense /* pool state */ pthread_t *threads;
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #3 |
| Registered User Join Date: Sep 2008
Posts: 44
| Hi thanks for the responce, well the program will do the following: it will take an unsorted input and writes it to a file in the proper order. The threads have their own local store of buffers that they have read, but otherwise share access to the source pipe, destination file, the index of the current entry to write, and the index of the last entry. The main function in the receiver will call two functions that create and manage threads for this project: thread_pool_init, which initializes the tpool_t data structure, and run_pool_threads, which creates the threads that run the function rcv_file. Initially, create one such thread, but ultimately six threads must be run. When a thread reads a line, the line is placed in a store. These stores are of limited size, so the you must ensure that a store does not overflow. If a store becomes full, then you must cause thread to wait until it has the next_entry line before it can proceed. I will make sure that the input file does not cause all stores to fill. Here is the psudo code: Code: rcv_file( input_ipc, outfile )
{
initialize( entry_store );
initialize( next_entry = 1 );
while ( more_to_receive && more_to_write ) {
if ( more_to_receive ) {
read_entry( input_ipc, entry_store );
determine if more_to_receive;
}
get lowest_read from entry_store;
while (TRUE) {
if ( lowest_read == next_entry ) {
write_entry( outfile, entry_store, lowest_read );
incr next_entry
get lowest_read
determine if more_to_write
if ( !more_to_write ) break
}
else break;
}
}
read_entry( input_ipc, entry_store )
{
initialize( buf, max_size );
bytes = mypipe_read( input_ipc, buf, max_size );
if ( buf has bytes ) {
get_index( buf, index );
check_for_eof( buf );
store_entry( buf, index, bytes, entry_store );
return index
}
else {
dealloc( buf )
return unusable index
}
}
write_entry( outfile, entry_store, lowest_read )
{
buf = get_entry_from_store( entry_store, lowest_read );
write( outfile, buf );
dealloc( buf );
}
He mentioned this: # With a single thread, you can put the mutual exclusion code in to protect the shared data structures. You must identify these shared data structures and add the appropriate pthreads code for mutual exclusion. The variables for mutual exclusion are provided in the data structure tpool_t. # Then, try two threads at a time. In this case, you will need to use a condition variable for a thread to wait and be awoken. The condition variable is also defined in tpool_t. So to answer your question, can that pointer be used to perhaps switch between threads in the pool thread? LIke that thread pointer will point to the active thread? Last edited by mr_coffee; 02-23-2009 at 12:46 PM. |
| mr_coffee is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| xor linked list | adramalech | C Programming | 23 | 10-14-2008 10:13 AM |
| data structure design for data aggregation | George2 | C# Programming | 0 | 05-20-2008 06:43 AM |
| [question]Analyzing data in a two-dimensional array[with code] | burbose | C Programming | 4 | 06-14-2005 05:45 AM |
| Deleting Data within a Structure or class | TankCDR | C++ Programming | 1 | 02-01-2002 10:37 PM |
| Serial Communications in C | ExDigit | Windows Programming | 7 | 01-09-2002 10:52 AM |