Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8927271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:10:59+00:00 2026-06-15T08:10:59+00:00

Hello I’m fallowing a tutorial for pthreading, but something went wrong down the road

  • 0

Hello I’m fallowing a tutorial for pthreading, but something went wrong down the road since can’t make the reference to my struct passable by
pthread_create(...,(void* stuctName)) , I’m looking for some advices or fixes since I have no idea where or what I messed up…
code:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <math.h>

struct dataBlock{
    struct node *root;
    int listSize;
    int forIndex;
};

struct node { // std linked list node
    int value;
    int worker;
    struct node *next;
};

int slots = 3; // only 3 threads are allowed to access the list
int availableCheck(){   // check if thread can acces the list
    if(slots < 3) return 0;
    else return -1;
}

pthread_mutex_t mutp = PTHREAD_MUTEX_INITIALIZER;   //condvar mutex
pthread_cond_t  condvar = PTHREAD_COND_INITIALIZER;   //condvar

void * worker( void *data ){ //WORKER FUNCTION
    printf( "* Thread start:            ^\n"); 
    struct dataBlock *inData = (struct dataBlock *) data;
    struct node *root = data->root;
    int listSize =  data->listSize;
    int forIndex = data ->forIndex;

    // printf( "    * I am %li _ worker  # %li : \n", forIndex, pthread_self() );

    pthread_mutex_lock( &mutp );
    if(availableCheck() < 0){
        printf( " ^^^ List not available yet... \n" ); 
        pthread_cond_wait( &condvar, &mutp );
    }
    // printf( "*        Got data:  %lu \n", index ); 
    pthread_cond_signal( &condvar ); // 
    pthread_mutex_unlock( &mutp ); 

    return NULL;
}

int main( int argc, char *argv[] ){
    if ( argc != 3 ){
        printf( "Programm must be called with \n NR of elements and NR of workers! \n " );
        exit( 1 );
    int i;
    struct node *root;
    struct node *iterator;  

//prepare list for task
    int listSize = atoi(argv[1]);
    int nrWorkers = atoi(argv[2]);
    root = malloc(sizeof( struct node) );
    root->value = rand() % 100;
    root->worker = 0;
    iterator = root;
    for( i=1; i<listSize; i++ ){
        iterator->next = malloc(sizeof(struct node));
        iterator = iterator->next;
        iterator->value = rand() % 100;
        iterator->worker = i % nrWorkers;
        printf("node #%d worker: %d  value: %d\n", i, iterator->worker,iterator->value);
    }

// Create all threads to parse the link list
    int ret, *id;   
    printf("workersInput: %d\n",nrWorkers);

    pthread_t w_thread;
    pthread_t* w_threads = malloc(nrWorkers * sizeof(w_thread));

    struct dataBlock *data = malloc(sizeof(struct dataBlock));
    data->root = root;
    data->listSize = listSize;

    for( i=0; i < nrWorkers; i++ ){ // CREATING THREADS
        data->forIndex = i;
        ret = pthread_create ( &w_threads[i], NULL, worker, (void *) data );
        if( ret ) {
            perror("Thread creation fail");
            exit(2);    
        }   
    } 
    for ( i = 0; i < nrWorkers; i++){
        pthread_join(w_threads[i],NULL);
    }
    free(root);
    free(iterator);
    return 0;
}

Compiling(Wall flag)

s.c: In function ‘worker’:
s.c:32:26: warning: dereferencing ‘void *’ pointer [enabled by default]
s.c:32:26: error: request for member ‘root’ in something not a structure or union
s.c:33:22: warning: dereferencing ‘void *’ pointer [enabled by default]
s.c:33:22: error: request for member ‘listSize’ in something not a structure or union
s.c:34:22: warning: dereferencing ‘void *’ pointer [enabled by default]
s.c:34:22: error: request for member ‘forIndex’ in something not a structure or union
s.c:34:6: warning: unused variable ‘forIndex’ [-Wunused-variable]
s.c:33:6: warning: unused variable ‘listSize’ [-Wunused-variable]
s.c:32:15: warning: unused variable ‘root’ [-Wunused-variable]
s.c:31:20: warning: unused variable ‘inData’ [-Wunused-variable]
s.c: In function ‘main’:
s.c:81:12: warning: variable ‘id’ set but not used [-Wunused-but-set-variable]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:11:00+00:00Added an answer on June 15, 2026 at 8:11 am

    Use inData->root instead of data->root etc… (or cast explicitly always your data) since data is a void* pointer, that is a pointer to some unspecified data type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello! I've been programming for a long time but just started developing for android,
Hello fellow developers... just to make sure, I want to ask this question: How
Hello can anybody solve this please I'm creating the object in the action class
. Hello, I am trying to make a Custom Segue for my storyboard. Now
hello creating a custom object may be a widely published topic, but my lack
Hello I am creating an application where I can store data in a Hashmap
Hello everyone how can I get two values from different select boxes? I get
hello i got a batch file, something like this: if %day%==monday, tuesday, wednesday, thursday,
Hello I'm stuck on a simple Java exercise, I hope someone can help. Sorry
Hello everybody i have asp.net mvc4 application where i made countdown something like this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.