linux - pThread 函数参数,向每个线程发送相同的值,等待其他内容

标签 linux pthreads pass-by-reference

我正在向我的线程发送一个结构数据 block ,其中包含指向相同 listRoot(对于所有线程)的指针,一个int indexInForLoop,和一个 int listSize;列表中的节点有一个要处理的 int 值和一个 int worker,它是 nrOfListElements % nrOfWorkers(Threads)。但不知何故,我认为我发送给每个线程的结构是相同的,但它不应该每个都应该有 1 个不同的 int 变量和 indexFromForLoop。我做错了什么?

终端输出:

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 

代码:

#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;
}

编辑:我使用了 pthread_self() 并确认存在不同的威胁

最佳答案

使用

data->forIndex = i;

您覆盖了以前的值。由于您只有一个 dataBlock,因此每个线程都将查找同一个位置。由于创建线程意味着“在执行循环中的下一条语句之前启动它”,每个线程看到的值纯属运气。

你必须为每个线程分配一个dataBlock,并且每个dataBLock只给一个线程。

关于linux - pThread 函数参数,向每个线程发送相同的值,等待其他内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13663872/

相关文章:

regex - unix 中正则表达式的语法错误

c++ - Linux 上 pthread 互斥体的默认优先级继承策略?

c++ - LRU 缓存和多线程

c# - 我应该使用 "ref"通过引用方法来传递集合(例如 List)吗?

C++ udp recvfrom 减少滴

linux - 在查找命令 Linux/Unix 中忽略文件列表

linux - expect Interact 一遍又一遍地读取匹配的行而不继续前进

c - 矩阵与线程相乘(每个线程做单次乘法)

c# - DataGridView:按值传递还是按引用传递?

javascript - 为什么不能将 null 设置为在 JavaScript 中通过引用传递的对象?