Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi
How can I create writer threads and reader pthreads in c++ which share global data?
Any help will be greatly appreciated.
Thanks

This should get you started, it is a few blocks of code pulled from an OS idea I had a few years ago.
#include <iostream.h>
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#include "myrandom.h"#define MAX 90000
// Shared data between threads
int myarr[MAX];#ifdef USE_LOCK
pthread_mutex_t global_lock;
#endifvoid *ProcessGroup1 ( void *arg );
int main ( int argc, char *argv[] )
{
*** populate array code ***
// Start the threads
void *res1, *res2, *res3;
pthread_t thr1, thr2, thr3;
double arg1, arg2, arg3;
arg1 = 1; arg2 = 2; arg3 = 3;#ifdef USE_LOCK
pthread_mutex_init( &global_lock, NULL );
#endifpthread_create( &thr1, NULL, ProcessGroup1, &arg1 );
pthread_create( &thr2, NULL, ProcessGroup2, &arg2 );
pthread_create( &thr3, NULL, ProcessGroup3, &arg3 );pthread_join( thr1, &res1 );
pthread_join( thr2, &res2 );
pthread_join( thr3, &res3 );#ifdef USE_LOCK
pthread_mutex_destroy( &global_lock );
#endif
*** Output code ***
}// Processing Group 1 of 3
void *ProcessGroup1 ( void *arg )
{
#ifdef USE_LOCK
pthread_mutex_lock( & global_lock );
#endif
sumGroup1 = PartialSum(0, FIRST_THIRD-1, myarr);
avgGroup1 = sumGroup1 / MAGIC_NUM;
#ifdef USE_LOCK
pthread_mutex_unlock( & global_lock );
#endif
return 0;
}*** Other Processing functions ***
*** Opeational functions ***

Well, my code formatting is not that bad guys... Guess I need to use the code posting deal on the forum...
#include <iostream.h>
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#include "myrandom.h"
#define MAX 90000
// Shared data between threads
int myarr[MAX];
#ifdef USE_LOCK
pthread_mutex_t global_lock;
#endif
void *ProcessGroup1 ( void *arg );
int main ( int argc, char *argv[] )
{
*** populate array code ***
// Start the threads
void *res1, *res2, *res3;
pthread_t thr1, thr2, thr3;
double arg1, arg2, arg3;
arg1 = 1; arg2 = 2; arg3 = 3;
#ifdef USE_LOCK
pthread_mutex_init( &global_lock, NULL );
#endif
pthread_create( &thr1, NULL, ProcessGroup1, &arg1 );
pthread_create( &thr2, NULL, ProcessGroup2, &arg2 );
pthread_create( &thr3, NULL, ProcessGroup3, &arg3 );
pthread_join( thr1, &res1 );
pthread_join( thr2, &res2 );
pthread_join( thr3, &res3 );
#ifdef USE_LOCK
pthread_mutex_destroy( &global_lock );
#endif
*** Output code ***
}
// Processing Group 1 of 3
void *ProcessGroup1 ( void *arg )
{
#ifdef USE_LOCK
pthread_mutex_lock( & global_lock );
#endif
sumGroup1 = PartialSum(0, FIRST_THIRD-1, myarr);
avgGroup1 = sumGroup1 / MAGIC_NUM;
#ifdef USE_LOCK
pthread_mutex_unlock( & global_lock );
#endif
return 0;
}
*** Other Processing functions ***
*** Opeational functions ***

Well.. that was really no better. At anyrate, before I over kill this post trying to get the stucture of my code readable... I'll leave it at that. I'm sure you can follow it.
Infinite Recursion

![]() |
Crystal Method
|
Creating a proxy server
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |