Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 递归函数与pthreads_C_Recursion_Pthreads - Fatal编程技术网

C 递归函数与pthreads

C 递归函数与pthreads,c,recursion,pthreads,C,Recursion,Pthreads,我最近开始研究pthreads,遇到了一个问题。因此,我有一个二叉树拓扑,15个节点(0->14个节点ID),我为节点3、4、5和6创建了4个pthread(不想为一个示例创建更多)。因此,创建的每个线程都试图到达其父节点以锁定节点并增加其中的全局变量的值。为此,我为节点结构中的每个节点创建了一个互斥体,并使用pthread_mutex_trylock条件——否则,我让他们做任何事情,例如,完成任务,不执行任务或彼此行为不检。所以,当我为每个线程调用一个函数时,我的程序崩溃了。代码如下所示。如果

我最近开始研究pthreads,遇到了一个问题。因此,我有一个二叉树拓扑,15个节点(0->14个节点ID),我为节点3、4、5和6创建了4个pthread(不想为一个示例创建更多)。因此,创建的每个线程都试图到达其父节点以锁定节点并增加其中的全局变量的值。为此,我为节点结构中的每个节点创建了一个互斥体,并使用pthread_mutex_trylock条件——否则,我让他们做任何事情,例如,完成任务,不执行任务或彼此行为不检。所以,当我为每个线程调用一个函数时,我的程序崩溃了。代码如下所示。如果我将函数testExclusion的名称更改为testExclusion22,则可以正常工作,否则程序将继续运行而不停止。如果我的自调用次数有限,也会发生同样的情况,例如3次循环->参见testFunction()。所以我的问题是,怎么了?我是否有写错的东西或这是不支持的,如果是,我怎么能这样做?非常感谢

//main.c

//  Threads_Extended


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

int globalVariable;
pthread_mutex_t global;

typedef struct node {    
    int value;
    int nodeID;
    struct node *parent;

    pthread_mutex_t nodeMutex;
} node_t ;


void initialize_node(node_t *node,node_t *next, int nodeIdentity)
{   
    node->nodeID=nodeIdentity;    
    node->parent = next;
    node->value=0;   
}


void printingFunction(void){
    printf("This is for testing ONLY!");

    //for (int i=0; i<3; i++) {
    printingFunction();
    //}
}


void testExclusion22(node_t *node, node_t *next)
{   
    int locked=0;    
    int locked2=0;

    printf("I am thread %d and I am trying to lock node %d!\n", node->nodeID, next->nodeID);

    while (locked!=1){
        if(pthread_mutex_trylock(&next->nodeMutex)==0){
            printf("I am thread %d and I am increasing the global value!\n", node->nodeID);

            while(locked2!=1){
                if ((pthread_mutex_trylock(&global))==0){
                    globalVariable++;
                    printf("Global variable's value is: %d!\n", globalVariable);
                    pthread_mutex_unlock(&global);
                    locked2=1;
                }
            }

            printf("I am thread %d and I am releasing the node %d!\n", node->nodeID, next->nodeID);
            pthread_mutex_unlock(&next->nodeMutex);
            locked=1;
        }
        else
            printf("I am thread %d and I was not able to lock next node %d!\n", node->nodeID, next->nodeID);
}

    printingFunction();
    // testExclusion(node, node->parent);
}

void testExclusion(node_t *node, node_t *next)
{
    int locked=0;
    int locked2=0;

    printf("I am thread %d and I am trying to lock node %d!\n", node->nodeID, next->nodeID);

    while (locked!=1){
        if(pthread_mutex_trylock(&next->nodeMutex)==0){
            printf("I am thread %d and I am increasing the global value!\n", node->nodeID);

            while(locked2!=1){
                if ((pthread_mutex_trylock(&global))==0){
                    globalVariable++;
                    printf("Global variable's value is: %d!\n", globalVariable);
                    pthread_mutex_unlock(&global);
                    locked2=1;
                }
            }

            printf("I am thread %d and I am releasing the node %d!\n", node->nodeID, next->nodeID);
            pthread_mutex_unlock(&next->nodeMutex);
            locked=1;
        }
        else
            printf("I am thread %d and I was not able to lock next node %d!\n", node->nodeID, next->nodeID);
    }

    testExclusion22(node, node->parent);
   // testExclusion(node, node->parent);
}



void *PrintHello(void *node)
{
    node_t *tempNode=node;
    pthread_mutex_init(&global, NULL);

    printf("Hello World! It's me, thread of node # %d!\n", (tempNode->nodeID));

    testExclusion(tempNode, tempNode->parent);   
    pthread_exit(NULL);
}


int main (int argc, char *argv[])
{

    node_t *nodes = malloc(15 * sizeof(node_t));

    int rc;
    int simCounter;

    int i;
    int j=0;
    int n=0;

    globalVariable=0;

    for (i=0; i<7; i++) {
        if (i==0){
            initialize_node(&nodes[i],NULL,i);
            printf("Node %d has been created! Node %d is the source root! \n\n", nodes[i].nodeID,nodes[i].nodeID);
        }
        else{
            if ((n%2)==0){
                initialize_node(&nodes[i],&nodes[j],i);
                printf("Node %d has been created with Node %d as a parent! \n\n", nodes[i].nodeID,nodes[j].nodeID);
                n++;
            }
            else
            {
                initialize_node(&nodes[i],&nodes[j],i);
                printf("Node %d has been created with Node %d as a parent! \n\n", nodes[i].nodeID,nodes[j].nodeID);
                j++;
                n++;
            }
        }
    }

    simCounter=2;

    for(i=0; i<7; i++)
        pthread_mutex_init(&nodes[i].nodeMutex, NULL);

    for (j=1; j<(simCounter+1); j++){
        pthread_t *threads = malloc(4 * sizeof(pthread_t));

        for(i=0; i<4; i++){
            printf("In main: creating thread %d\n", i);
            nodes[i].nodeID=i;
            rc = pthread_create(&threads[i], NULL, PrintHello, (void *)&nodes[i+3]);
            if (rc){
                printf("ERROR; return code from pthread_create() is %d\n", rc);
                exit(-1);
            }
            pthread_mutex_destroy(&nodes[i+3].nodeMutex);
        }
        for(i=0; i<4; i++)
            pthread_join(threads[i], NULL);

        printf("I am back in main!\n\n\n");

        free(threads);
        pthread_mutex_destroy(&global);
    }

    free(nodes);
    pthread_exit(NULL);
}
//线程\u扩展
#包括
#包括
#包括
#包括
整数全局变量;
pthread_mutex_t global;
typedef结构节点{
int值;
int-nodeID;
结构节点*父节点;
pthread_mutex_t nodeMutex;
}节点t;
void initialize_node(node_t*node,node_t*next,int nodeIdentity)
{   
节点->节点ID=nodeID实体;
节点->父节点=下一步;
节点->值=0;
}
void打印函数(void){
printf(“这仅用于测试!”);
//for(int i=0;inodeID,next->nodeID);
while(已锁定!=1){
if(pthread\u mutex\u trylock(&next->nodeMutex)==0){
printf(“我是线程%d,正在增加全局值!\n”,节点->节点ID);
while(锁定2!=1){
if((pthread\u mutex\u trylock(&global))==0){
全局变量++;
printf(“全局变量的值为:%d!\n”,全局变量);
pthread_mutex_unlock(&global);
锁定2=1;
}
}
printf(“我是线程%d,我正在释放节点%d!\n”,节点->节点ID,下一步->节点ID);
pthread_mutex_unlock(&next->nodeMutex);
锁定=1;
}
其他的
printf(“我是线程%d,无法锁定下一个节点%d!\n”,节点->节点ID,下一个->节点ID);
}
打印函数();
//testExclusion(节点,节点->父节点);
}
void testExclusion(node_t*node,node_t*next)
{
int-locked=0;
int locked2=0;
printf(“我是线程%d,正在尝试锁定节点%d!\n”,节点->节点ID,下一步->节点ID);
while(已锁定!=1){
if(pthread\u mutex\u trylock(&next->nodeMutex)==0){
printf(“我是线程%d,正在增加全局值!\n”,节点->节点ID);
while(锁定2!=1){
if((pthread\u mutex\u trylock(&global))==0){
全局变量++;
printf(“全局变量的值为:%d!\n”,全局变量);
pthread_mutex_unlock(&global);
锁定2=1;
}
}
printf(“我是线程%d,我正在释放节点%d!\n”,节点->节点ID,下一步->节点ID);
pthread_mutex_unlock(&next->nodeMutex);
锁定=1;
}
其他的
printf(“我是线程%d,无法锁定下一个节点%d!\n”,节点->节点ID,下一个->节点ID);
}
testExclusion22(节点,节点->父节点);
//testExclusion(节点,节点->父节点);
}
void*PrintHello(void*node)
{
node_t*tempNode=节点;
pthread_mutex_init(&global,NULL);
printf(“你好,世界!是我,节点#%d!\n”的线程,(tempNode->nodeID));
testExclusion(tempNode,tempNode->parent);
pthread_exit(NULL);
}
int main(int argc,char*argv[])
{
node_t*nodes=malloc(15*sizeof(node_t));
int rc;
int模拟计数器;
int i;
int j=0;
int n=0;
全局变量=0;

因为(i=0;i您的
printingFunction()
永远在调用自己,没有任何退出条件:

void printingFunction() {
    printf("This is for testing ONLY!");
    printingFunction();
}

如果不是很明显,在互斥锁上旋转一个try锁有点弄巧成拙。仍然在看代码的其余部分,但是呃。为什么要旋转?为什么不直接调用
phtread\u mutex\u lock()
没有
循环?我强烈建议您重新了解pthread及其细微差别和用法。当我看到类似的内容时:
pthread_exit(NULL);
位于
main()的底部
我开始怀疑。而且这些循环中的任何一个都不需要旋转。谢谢你的评论。我已经更改了下面的函数并放弃了所有其余的函数。尽管如此,仍然是一样的。因此,在trylock中使用if条件,因为我的互斥锁没有按应该的方式锁定,但仍然在它们之间使用代码,导致内存错误,e、 空指针等。我需要确保每次都有人在使用我的变量。pthread_exit(null)在本教程的主要原因结尾处:。pthreads的新功能,我必须从某个地方开始学习。