Computing.Net > Forums > Programming > creation of Thread using c++

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

creation of Thread using c++

Reply to Message Icon

Name: priti
Date: August 6, 2003 at 22:02:23 Pacific
OS: win 2000
CPU/Ram: P4
Comment:

Hi
How can I create writer threads and reader pthreads in c++ which share global data?
Any help will be greatly appreciated.
Thanks



Sponsored Link
Ads by Google

Response Number 1
Name: Infinite Recursion
Date: August 6, 2003 at 22:33:45 Pacific
Reply:

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;
#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 ***



0

Response Number 2
Name: Infinite Recursion
Date: August 6, 2003 at 22:36:41 Pacific
Reply:

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


0

Response Number 3
Name: Infinite Recursion
Date: August 6, 2003 at 22:37:41 Pacific
Reply:

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


0

Response Number 4
Name: priti
Date: August 6, 2003 at 23:24:46 Pacific
Reply:

Thank you very much for very detailed answer.
I really appreciate that.


0

Response Number 5
Name: Infinite Recursion
Date: August 7, 2003 at 02:05:43 Pacific
Reply:

You're welcome Priti. Good luck with Pthreads, they're fairly interesting.

Infinite Recursion


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon

Crystal Method Creating a proxy server



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: creation of Thread using c++

equation of parabola in c++ program www.computing.net/answers/programming/equation-of-parabola-in-c-program/19724.html

Declairation Of Socket In C www.computing.net/answers/programming/declairation-of-socket-in-c/2683.html

multi-threaded in C www.computing.net/answers/programming/multithreaded-in-c/2289.html