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

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

creation of Thread using c++

Reply to Message Icon

Original Message
Name: priti
Date: August 6, 2003 at 22:02:23 Pacific
Subject: creation of Thread using c++
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


Report Offensive Message For Removal


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

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



Report Offensive Follow Up For Removal

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

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


Report Offensive Follow Up For Removal

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

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


Report Offensive Follow Up For Removal

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

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


Report Offensive Follow Up For Removal

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

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

Infinite Recursion


Report Offensive Follow Up For Removal







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








Do you own an iPhone?

Yes
No, but soon
No


View Results

Poll Finishes In 6 Days.
Discuss in The Lounge
Poll History




Data Recovery Software