Sponsored Content
Full Discussion: Threads help
Top Forums Programming Threads help Post 302316606 by smurf2 on Friday 15th of May 2009 01:51:57 PM
Old 05-15-2009
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);

}

 

9 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

old threads

Neo, if I have an old thread that is a few months old, and a few pages back in the forum it was posted in, is it ok to 'bump' it back to the front? or, would you rather i deleted the old thread, and just create a new one? btw the thread has no replies. (2 Replies)
Discussion started by: norsk hedensk
2 Replies

2. UNIX for Dummies Questions & Answers

threads

i am tring to sort lots of data thats in many columns by just one column but, if I use sort +16 inputfile the column fluctuates because some of the rows have spaces etc within the text, so the end result is just a mess as it jumps around the columns depending whether it has spaces or not ....ie... (2 Replies)
Discussion started by: Gerry405
2 Replies

3. Solaris

threads

Hi all! 1)Is there a way to write a program that will work on both solaris and intel based machines. 2)How can I achive this for a program that creates and synchronizes three threads. Thank you. vij. (3 Replies)
Discussion started by: vijlak
3 Replies

4. UNIX for Advanced & Expert Users

How many threads do I use ?

Hi, I have a program that has two types of threads: 1) Reader threads 2) Worker Threads Readers: Their only job is to read files. They just read data from the files and put them into a buffer. They are obviously I/O intensive. Workers: These are CPU intensive. They do some computation... (5 Replies)
Discussion started by: the_learner
5 Replies

5. UNIX for Advanced & Expert Users

Threads and Threads Count ?

Hi all, How can I get the list of all Threads and the Total count of threads under a particular process ? Do suggest !! Awaiting for the replies !! Thanks Varun:b: (2 Replies)
Discussion started by: varungupta
2 Replies

6. Programming

problem with threads in C

I have problem that if I create for example 100 threads program work correctly but if I define more threads for example 1000 // if I change static int NUM_E from 100 to 1000 than program stop about 350 threads and doesn't continue where should be problem please? #include <pthread.h>... (4 Replies)
Discussion started by: Sevco777
4 Replies

7. Programming

Java Threads

Hi guys, I want to start studding about java threads. but the only book available on the market is "Java Threads" by O'Reilly... (0 Replies)
Discussion started by: majid.merkava
0 Replies

8. What is on Your Mind?

Why not more DEVOPS threads?

Hello all, searching for answers about our upcomming infrastructure in my compagny i was sadden by the fact that in my favorite programming forum there was basicly no threads about devops subjects or continious integration platform. Would be fun to know people workflows in their compagny and... (0 Replies)
Discussion started by: maverick72
0 Replies

9. Post Here to Contact Site Administrators and Moderators

Threads disappeared

Dear admins, it seems that some threads or even users have recently (~ 2 days or so) disappeared. Examples: giuliangiuseppe and greycells. The latter asked me for the solution I provided earlier this week. What happened? Can you help? Regards Rüdiger (10 Replies)
Discussion started by: RudiC
10 Replies
PTHREAD_SETNAME_NP(3)					     Linux Programmer's Manual					     PTHREAD_SETNAME_NP(3)

NAME
pthread_setname_np, pthread_getname_np - set/get the name of a thread SYNOPSIS
#define _GNU_SOURCE /* See feature_test_macros(7) */ #include <pthread.h> int pthread_setname_np(pthread_t thread, const char *name); int pthread_getname_np(pthread_t thread, const char *name, size_t len); Compile and link with -pthread. DESCRIPTION
By default, all the threads created using pthread_create() inherit the program name. The pthread_setname_np() function can be used to set a unique name for a thread, which can be useful for debugging multithreaded applications. The thread name is a meaningful C language string, whose length is restricted to 16 characters, including the terminating null byte (''). The thread argument specifies the thread whose name is to be changed; name specifies the new name. The pthread_getname_np() function can be used to retrieve the name of the thread. The thread argument specifies the thread whose name is to be retrieved. The buffer name is used to return the thread name; len specifies the number of bytes available in name. The buffer spec- ified by name should be at least 16 characters in length. The returned thread name in the output buffer will be null terminated. RETURN VALUE
On success, these functions return 0; on error, they return a nonzero error number. ERRORS
The pthread_setname_np() function can fail with the following error: ERANGE The length of the string specified pointed to by name exceeds the allowed limit. The pthread_getname_np() function can fail with the following error: ERANGE The buffer specified by name and len is too small to hold the thread name. If either of these functions fails to open /proc/self/task/[tid]/comm, then the call may fail with one of the errors described in open(2). VERSIONS
These functions first appeared in glibc in version 2.12. CONFORMING TO
These functions are nonstandard GNU extensions. NOTES
pthread_setname_np() internally writes to the thread specific comm file under /proc filesystem: /proc/self/task/[tid]/comm. pthread_get- name_np() retrieves it from the same location. EXAMPLE
The program below demonstrates the use of pthread_setname_np() and pthread_getname_np(). The following shell session shows a sample run of the program: $ ./a.out Created a thread. Default name is: a.out The thread name after setting it is THREADFOO. ^Z # Suspend the program [1]+ Stopped ./a.out $ ps H -C a.out -o 'pid tid cmd comm' PID TID CMD COMMAND 5990 5990 ./a.out a.out 5990 5991 ./a.out THREADFOO $ cat /proc/5990/task/5990/comm a.out $ cat /proc/5990/task/5991/comm THREADFOO Program source #define _GNU_SOURCE #include <pthread.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <errno.h> #include <stdlib.h> #define NAMELEN 16 #define errExitEN(en, msg) do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) static void * threadfunc(void *parm) { sleep(5); // allow main program to set the thread name return NULL; } int main(int argc, char **argv) { pthread_t thread; int rc; char thread_name[NAMELEN]; rc = pthread_create(&thread, NULL, threadfunc, NULL); if (rc != 0) errExitEN(rc, "pthread_create"); rc = pthread_getname_np(thread, thread_name, NAMELEN); if (rc != 0) errExitEN(rc, "pthread_getname_np"); printf("Created a thread. Default name is: %s ", thread_name); rc = pthread_setname_np(thread, (argc > 1) ? argv[1] : "THREADFOO"); if (rc != 0) errExitEN(rc, "pthread_setname_np"); sleep(2); rc = pthread_getname_np(thread, thread_name, (argc > 2) ? atoi(argv[1]) : NAMELEN); if (rc != 0) errExitEN(rc, "pthread_getname_np"); printf("The thread name after setting it is %s. ", thread_name); rc = pthread_join(thread, NULL); if (rc != 0) errExitEN(rc, "pthread_join"); printf("Done "); exit(EXIT_SUCCESS); } SEE ALSO
prctl(2), pthread_create(3), pthreads(7) COLOPHON
This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2013-06-21 PTHREAD_SETNAME_NP(3)
All times are GMT -4. The time now is 01:51 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy