0

我想用互斥锁完成我的线程。第一个线程不会执行,线程 2 和 3 执行。

有谁知道这个问题可能是什么?有时线程 1 已执行,但随后 2 或 3 未执行。我不知道这里有什么问题。

Thread created successfully
    Thread created successfully
    Thread created successfully
    ----------------------------------------------------
    J:0
    NUM_REQUESTS (before function): 0
    J:0
    ----------------------------------------------------
    ----------------------------------------------------
    J:1
    Third thread processing done
    WRITE DATA TO LIST!
    NUM_REQUESTS(function): 1
    NUM_REQUESTS (before function): 0
    J:1
    ----------------------------------------------------
    ----------------------------------------------------
    J:2
    Second thread processing done
    WRITE DATA TO LIST!
    NUM_REQUESTS(function): 0
    NUM_REQUESTS (before function): 0
    J:2
    ----------------------------------------------------

程序:

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

pthread_mutex_t request_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t got_request = PTHREAD_COND_INITIALIZER;

pthread_t tid[3];
int thr_id[3];
int ret1,ret2,ret3;
int i = 0;
int err;
int *ptr[2];
int num_requests = 0;
int rc = 0;

这是线程的功能,第一个线程没有执行!

void* doSomeThing(void *arg)
{
    unsigned long i = 0;
    pthread_t id = pthread_self();

    for(i=0; i<1000000;i++);

    if(pthread_equal(id,tid[0]))
    {
         printf("First thread processing done\n");
         printf("WRITE DATA TO LIST!\n");
         num_requests--;
         printf("NUM_REQUESTS(function): %d\n",num_requests);
         rc = pthread_mutex_unlock(&request_mutex);
         pthread_exit(&tid[0]);
    }else if(pthread_equal(id,tid[1])){

        printf("Second thread processing done\n");
        num_requests--;
        printf("WRITE DATA TO LIST!\n");
        printf("NUM_REQUESTS(function): %d\n",num_requests);
        rc = pthread_mutex_unlock(&request_mutex);
        pthread_exit(&tid[1]);
    }else if(pthread_equal(id,tid[2])){

        printf("Third thread processing done\n");
        printf("WRITE DATA TO LIST!\n");
        printf("NUM_REQUESTS(function): %d\n",num_requests);
        num_requests--;
        rc = pthread_mutex_unlock(&request_mutex);
        pthread_exit(&tid[2]);
    }
    return NULL;
}

这是我创建线程输出的地方

void add_request(int j,pthread_mutex_t* p_mutex,pthread_cond_t* p_cond_var)
{
    printf("----------------------------------------------------\n");
    printf("J:%d\n",j);
    if(num_requests > 3){
        printf("WAIT TILL THREADS ARE FREE!\n");
    }else{

    rc = pthread_mutex_lock(&request_mutex);
    printf("NUM_REQUESTS (before function): %d\n",num_requests);    

    num_requests++;
    rc = pthread_mutex_unlock(&request_mutex);

    rc = pthread_mutex_lock(&request_mutex);
    rc = pthread_cond_signal(p_cond_var);
    printf("J:%d\n",j);
    printf("----------------------------------------------------\n");
    }
}

在主要我只创建线程并使用 add_request 函数来执行线程

int main(void)
{
    //create 3 threads
    while(i < 3)
    {
    thr_id[i] = i;
        err = pthread_create(&(tid[i]), NULL, &doSomeThing, (void*)&thr_id[i]);
        if (err != 0)
            printf("can't create thread :[%s]", strerror(err));
        else
            printf("Thread created successfully\n");

        i++;
    }    

    int j;
    for(j=0;j<3;j++){
    add_request(j, &request_mutex, &got_request);
    }


    return 0;
}
4

1 回答 1

1

你想做什么?

至少有一个问题,我看到你解锁互斥锁而不锁定它......

当您创建一个线程时,它会自发启动(或多或少),因此您不能假设 add_request 将在 doSomething() 之后调用

您可以锁定然后解锁。ptheard_join如果您想等到线程完成,也可以考虑使用。

编辑 这是你想要做的https://computing.llnl.gov/tutorials/pthreads/samples/condvar.c -取自https://computing.llnl.gov/tutorials/pthreads/

祝你好运

于 2013-05-12T09:02:45.543 回答