The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Threads and Threads Count ? varungupta UNIX for Advanced & Expert Users 2 03-21-2008 09:23 PM
How many threads do I use ? the_learner UNIX for Advanced & Expert Users 5 07-17-2007 05:31 PM
threads vijlak SUN Solaris 3 01-29-2007 07:55 PM
old threads norsk hedensk Post Here to Contact Site Administrators and Moderators 2 06-03-2003 05:16 PM
nfs threads i2admin UNIX for Advanced & Expert Users 1 03-06-2003 11:28 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-15-2009
smurf2 smurf2 is offline
Registered User
  
 

Join Date: May 2009
Posts: 2
Threads help

Hello! I started studying studying about POSIX Threads a few days ago... so I am a little confused and I would appreciate some help!

I isolated this code... and I wonder if I could use threads in it!

Code:
#include <unistd.h>
#endif

#include <math.h>
//#include "main.h"
#include <sys/time.h>
#include <time.h>


#define TEMPRANGE 100.0
#define ROOMTEMP 0.0

volatile double ***before;
volatile double ***after;

// x-axis
const int cols = 240;
// y-axis
const int rows = 80;
// z-axis
const int faces = 8;


int main(int argc, char** argv){

  char buffer[30];
  struct timeval tv;
  time_t curtime;

  gettimeofday(&tv, NULL); 
  curtime=tv.tv_sec;

  strftime(buffer,30,"%m-%d-%Y  %T.",localtime(&curtime));
  printf("%s%ld\n",buffer,tv.tv_usec);

    int i, j, k;
    before = (volatile double ***) malloc((cols + 2) * sizeof(double **));
    after = (volatile double ***) malloc((cols + 2) * sizeof(double **));

    for(i = 0; i < cols + 2; i++){
        before[i] = (volatile double **) malloc((rows + 2) * sizeof(double *));
        after[i] = (volatile double **) malloc((rows + 2) * sizeof(double *));
    }

    for(i = 0; i < cols + 2; i++){
        for(j = 0; j < rows + 2; j++){
            before[i][j] = (volatile double *) malloc((faces + 2) * sizeof(double));
            after[i][j] = (volatile double *) malloc((faces + 2) * sizeof(double));
        }
    }

    for(i = 0; i < cols + 2; i++){
        for(j = 0; j < rows + 2; j++){
            for(k = 0; k < faces + 2; k++){
                before[i][j][k] = ROOMTEMP;
                after[i][j][k] = ROOMTEMP;
            }
        }
    }

    char buffer1[30];
  time_t curtime1;
  gettimeofday(&tv, NULL); 
  curtime1=tv.tv_sec;

  strftime(buffer1,30,"%m-%d-%Y  %T.",localtime(&curtime1));
  printf("%s%ld\n",buffer1,tv.tv_usec);

}
I was thinking of putting the three for loops in 3 different threads and keep in the main the other code and use pthread_join in main for each one of the 3 threads. I hope you understand my way of thinking... Am I thinking right?
Thank you in advance!
  #2 (permalink)  
Old 05-15-2009
smurf2 smurf2 is offline
Registered User
  
 

Join Date: May 2009
Posts: 2
Here is my threade code... If anybody can take a look and tell me if there is sth wrong I would appreciate it very much!

Code:
#ifdef linux
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#endif

#include <math.h>
//#include "main.h"
#include <sys/time.h>
#include <time.h>

#define TEMPRANGE 100.0
#define ROOMTEMP 0.0
#define SOURCETEMP 100.0

// Plate temperatures for the jacobi calculation
volatile double ***before;
volatile double ***after;

// x-axis
const int cols = 240;
// y-axis
const int rows = 80;
// z-axis
const int faces = 8;


//thread function1
void *thread_function1 (void *arg){
    before = (volatile double ***) malloc((cols + 2) * sizeof(double **));
    after = (volatile double ***) malloc((cols + 2) * sizeof(double **));
}

//thread function2
void *thread_function2 (void *arg){
    for(int i = 0; i < cols + 2; i++){
        before[i] = (volatile double **) malloc((rows + 2) * sizeof(double *));
        after[i] = (volatile double **) malloc((rows + 2) * sizeof(double *));
    }
}

//thread function3
void *thread_function3 (void *arg){
    for(int i = 0; i < cols + 2; i++){
        for(int j = 0; j < rows + 2; j++){
            before[i][j] = (volatile double *) malloc((faces + 2) * sizeof(double));
            after[i][j] = (volatile double *) malloc((faces + 2) * sizeof(double));
        }
    }

}


int main(int argc, char** argv){

  char buffer[30];
  struct timeval tv;
  time_t curtime;
  pthread_t mythread1;
  pthread_t mythread2;
  pthread_t mythread3;

  gettimeofday(&tv, NULL); 
  curtime=tv.tv_sec;

  strftime(buffer,30,"%m-%d-%Y  %T.",localtime(&curtime));
  printf("%s%ld\n",buffer,tv.tv_usec);

    int i, j, k;

    if ( pthread_create( &mythread1, NULL, thread_function1, NULL) ) {
    printf("error creating thread.");
    abort();
  }

      if ( pthread_create( &mythread2, NULL, thread_function2, NULL) ) {
    printf("error creating thread.");
    abort();
  }

        if ( pthread_create( &mythread3, NULL, thread_function3, NULL) ) {
    printf("error creating thread.");
    abort();
  }
          if ( pthread_join ( mythread1, NULL ) ) {
    printf("error joining thread.");
    abort();
  }
            if ( pthread_join ( mythread2, NULL ) ) {
    printf("error joining thread.");
    abort();
  }
              if ( pthread_join ( mythread3, NULL ) ) {
    printf("error joining thread.");
    abort();
  }

    for(i = 0; i < cols + 2; i++){
        for(j = 0; j < rows + 2; j++){
            for(k = 0; k < faces + 2; k++){
                before[i][j][k] = ROOMTEMP;
                after[i][j][k] = ROOMTEMP;
            }
        }
    }

    char buffer1[30];
  time_t curtime1;

  gettimeofday(&tv, NULL); 
  curtime1=tv.tv_sec;

  strftime(buffer1,30,"%m-%d-%Y  %T.",localtime(&curtime1));
  printf("%s%ld\n",buffer1,tv.tv_usec);

}
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:20 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0