Thread: Threads and all that is wrong with them

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    2

    Question Threads and all that is wrong with them

    Okay, I've got an interesting problem...
    I have a threaded program, and each thread has several variables that need to be passed into it. Several ints, a float or two, and a matrix. Now, I've gotten my struct built so that I can pass in all these very odd values:

    struct thread_struct{
    int rows;
    int cols;
    int sem_int;
    float *matrix;
    };

    //Insert some code in here

    float *t_matrix[i][j];
    thread_struct *t_type;
    (*t_type).cols=j;
    (*t_type).rows=i;
    (*t_type).sem_int=7;
    (*t_type).matrix=(float*)t_matrix;

    And then I pass the struct into my thread

    pthread_create(&pde_t[0], NULL, t_proc, (void*)&t_type);

    Now, my question is HOW do I retrieve all of my info once I get into my thread?

    Thanks y'all.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    2
    Thanks for your help. Now I just gotta code it.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Um... the structure needs to be dynamic memory, pthread_create only passes a void*:

    Code:
    typedef struct mydata {
        int some_value;
        char buffer[32];
        float etc;
        double just_because;
    } MyData;
    
    void* funct(void* arg)
    {
        MyData* pdata = (MyData*)arg;
    
        // do something
    
        // free data 
        free(pdata);
    
        return NULL; // this value can be picked up by pthread_join()
    }
    
    int main()
    {
        pthread_t thr;
        MyData* dyn_data;
        int sts;
        void* ret_sts = NULL;
    
        dyn_data = malloc(sizeof(MyData));
    
        // Fill the members
    
        // call pthread_create
        sts = pthread_create(&thr, NULL, funct, dyn_data);
        if ( sts < 0 )
        { /* complain */ }
    
        // wait for thread to complete
         pthread_join(thr, &ret_sts);
    
        return 0;
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

Popular pages Recent additions subscribe to a feed