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 8933827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:38:16+00:00 2026-06-15T09:38:16+00:00

I am sending a sturct dataBlock to my threads which contains a pointer to

  • 0

I am sending a sturct dataBlock to my threads which contains a pointer to same listRoot(for all threads), an int indexInForLoop, and an int listSize; The Node from the list has a int value which ash to be processed and a int worker which is nrOfListElements % nrOfWorkers(Threads). But somehow I thing the struct I send to each thread is the same, but it shouldn’t each should have 1 different int variable with the indexFromForLoop. What am I doing wrong?

terminal output:

node #1 worker: 1  value: 86
node #2 worker: 0  value: 77
node #3 worker: 1  value: 15
node #4 worker: 0  value: 93
node #5 worker: 1  value: 35
? List got populated
workersInput: 2
in for, ret= 0
in for, ret= 0
* Thread start:             id: 3067579200^
forID_ 1 
->val: 86   
valid for sqrt 
* Thread start:             id: 3075971904^
forID_ 1 
->val: 86   
valid for sqrt 

Code:

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

#define workTime 5
#define workersLimitNr 3

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

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

int slots = 0; // 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 ){
    struct dataBlock *inData = (struct dataBlock *) data;
    struct node *root = inData->root;
    int listSize =  inData->listSize;
    int forIndex = inData ->forIndex;

    printf( "* Thread start:            ^\n"); 

    pthread_mutex_lock( &mutp );
    if(availableCheck() < 0){
        printf( " ^^^ List not available yet... \n" ); 
        pthread_cond_wait( &condvar, &mutp );
    } 
    struct node *it = root;
    printf("forID_ %d \n", forIndex);
    while(it->next != NULL){
        if(forIndex == it->worker){
            printf("valid for sqrt \n");
            if(it->value > 2){
                sqrt(it->value);
                break;
            }
        }
        it = it->next;
        printf("->val: %d   \n", it->value);
    }

    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);
    }
    printf("? List got populated\n");

// Create all threads to parse the link list
    int ret;    
    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++ ){ 
        data->forIndex = i;
        ret = pthread_create ( &w_threads[i], NULL, worker, (void *) data );
        if( ret ) {
            perror("Thread creation fail");
            exit(2);    
        }   
        printf("in for, ret= %d\n",ret);
    } 

    for ( i = 0; i < nrWorkers; i++){
        pthread_join(w_threads[i],NULL);
    }

    free(root);
    free(iterator);
    return 0;
}

EDIT: I used a pthread_self() and I confirm there are different threats

  • 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-15T09:38:16+00:00Added an answer on June 15, 2026 at 9:38 am

    Using

    data->forIndex = i;
    

    you overwrite the previous value(s). And since you have only one dataBlock each thread will look to the same place. And since creating a thread does not mean “start it before the next statement in the loop is executed” it is pure luck what value each thread sees.

    You have to allocate one dataBlock for each thread and give each dataBLock only to one thread.

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

Sidebar

Related Questions

Sending the same pointer into two different shared_ptr 's is bad, it causes double
sending text to label and number to int from tabelview to viewcontroller but it
I sending ajax request from a jQuery file like below, which expects response in
about the code: tp is a pointer to a certain struct which contains a
I have a CFC file which handles all of the emails I'm sending form
I'm sending from a .Net application 1404 float values which make up for 5616
Sending XML from C# Server and receiving it it in Android Java client This
When sending data from one iOS device to another, I receive these errors. 2012-06-21
I have a code which receives packets from the Ethernet and sends it via
I'm sending a C struct over UDP struct packet{ int numInt; int* intList; //malloc'ed

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.