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

  • SEARCH
  • Home
  • 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 8915779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:03:19+00:00 2026-06-15T05:03:19+00:00

In order to write this code I checked and used some tutorials, still somewhere

  • 0

In order to write this code I checked and used some tutorials, still somewhere I failed, considering that only the main function seems to work and exits with code 0. Aren’t my treads created? They are but somehow I failed to link them to my “working” function?

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


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

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 *i ){
    long index = (long)i;
    printf( "    * I am worker  # %lu : \n",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,listSize,workersSize;
    struct node *root;
    struct node *iterator;  

//prepare list for task
    listSize = atoi(argv[1]);
    root = malloc(sizeof( struct node) );

    root->value = rand() % 100;
    // printf(">>> %d\n", root->value );
    root->worker = 0;

    iterator = malloc(sizeof(struct node) );
    iterator = root;

    for( i=1; i<listSize; i++ ){
        iterator->next = malloc(sizeof(struct node));
        iterator = iterator->next;
        iterator->value = rand() % 100;
        iterator->worker = i;
        printf("node #%d worker: %d  value: %d\n", i, iterator->worker,iterator->value);
    }
    printf("? List got populated\n");

// Create all threads to parse the link list
    int ret;    
    pthread_t w_thread;
    int nrWorkers = atoi(argv[2]);
    pthread_t* w_threads = malloc(nrWorkers * sizeof(w_thread));
    for( i=0; i<listSize; i++ ){
        int *id = malloc(sizeof(int));
        *id = i;
         ret = pthread_create ( &w_threads[i], NULL, worker, (void *) &id );
         if( ret ) {
            perror("Thread creation fail");
            exit(2);    
        }
    } 
    int j;
    for (j = 0; i < nrWorkers; j++){
        pthread_join(w_threads[j],NULL);
    }       
}

Ps some of the commented functions / variables are going to be used /implemented but at this point i expect the threads

  • 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-15T05:03:20+00:00Added an answer on June 15, 2026 at 5:03 am

    I suspect that in for loop in which you are creating threads you should have:

     for( i=0; i<nWorkers; i++ ){
    

    Also, I would suggest you to add checks/prints for input arguments.

    And one more issue: you pass pointer to worker(), so you have to de-reference the pointer, and if it’s pointer to int, then

    void * worker( void *p_int ){
        assert(p_int != NULL)   
        int index = *(int*)p_ind;
    

    Be accurate with casting from void*!

    And one last thing. If you know listSize, it’s not clear for me why are you trying to create the linked list:

    for( i=1; i<listSize; i++ ){
        iterator->next = malloc(sizeof(struct node));
        iterator = iterator->next;
        ...
    

    Instead, you can allocate your array just in one line of code and don’t need to allocation for root:

    p_list = (struct node*)malloc(listSize*sizeof(struct node));
    assert(p_list);
    for( i=0; i<listSize; i++ ){
        p_list[i].value = ...
        ...
    

    As you see, you don’t need next pointer here.

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

Sidebar

Related Questions

I am trying to write a higher-order Racket function that takes a first-order function
I'm trying to write a named scope that will order my 'Products' class based
I wanted to write a higher order function filter with C++. The code I
Can anyone help me write this query more efficiently? I have a table that
Just wrote some nasty code that iterates over a dict or a list in
Debugging with gdb, any c++ code that uses STL/boost is still a nightmare. Anyone
I created a web page for viewing images. This page has some other code
I was reading this article about Double-Checked locking and out of the main topic
I'm having an issue with some of my code executing in an unexpected order.
I found this code here that i think will do the job. `/* suppose

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.