1

嘿伙计们,我想知道是否有人可以提供一点帮助。

我一直在尝试自学 pthreads 和互斥锁,以使线程一起运行并使用相同的结构,而不是读取和写入坏数据。

我现在的问题是,

从我的线程函数中,如果我调用一个可能类似于以下内容的辅助函数:

void foo(void *arg)
{
  Bar *bar = arg;
  pthread_mutex_lock(&mutex);
  bar->something = 1;
  pthread_mutex_unlock(&mutex);
}

上面的这个辅助方法似乎并没有“更新”结构。

但是,如果我在线程函数中运行相同的代码,完全相同的 4 行,那么这似乎可以工作。

我究竟做错了什么?或者我该如何解决这个问题?如果有人也可以提供一些阅读,那将是完美的。

编辑:对不起,这是我的代码中的错字。

这是我用于结构的实际代码。

typedef struct {
    char *buffer[CAR_PARK_SIZE];       
    char *arrival_time[CAR_PARK_SIZE]; 
    int  keep_running;           
    int  size;          
 int  index;     
 } CarStorage;

typedef struct {
 CarStorage parks;
 CarStorage queue;
 int busy;
 } CarPark;

pthread_mutex_t mutex;

void addCar(char *car, void *arg)
{
 CarPark *_cp = arg;
 pthread_mutex_lock(&mutex);
 printf("Trying to increase size\n");
 _cp->parks.size = _cp->parks.size+1;
 pthread_mutex_unlock(&mutex);
}

如果 addCar 中的相同行在线程函数中,它会增加大小,如果它在这个辅助函数中,则不会。

这是调用代码

void *carpark_t(void *arg)
{
    CarPark *_cp = arg; 
    while (_cp->parks.keep_running)
    {

        if (_cp->queue.size > 0)
        {

            addCar(_cp->queue.buffer[_cp->queue.index % MAX_QUEUE], &_cp);
            sleep(1);
        }
        else
        {
            printf("[C] no cars in queue\n");
            sleep(5);
        }
    }

}
4

2 回答 2

4

----被剪断,因为它不再适用并且无论如何都不起作用----

---- 剪掉了一些,因为它不再适用并且无论如何都不起作用----

这是你的错误:

            addCar(_cp->queue.buffer[_cp->queue.index % MAX_QUEUE], &_cp);

&_cp就是传递 的地址 _cp,它是指向 的指针_cp。但是_cp已经是一个指针,所以你传入一个指向指针的指针。要么更改&_cp为常规_cp,要么更改void addCar(char *car, void *arg)void addCar(char *car, void **arg)(并addCar()相应地进行编辑)。任何一个都应该工作,但我推荐第一个,因为它更容易。

于 2009-09-04T02:19:58.343 回答
1

What you're doing in addCar with the locking is fine. Your problem is somewhere in the code that you haven't posted. Without access to that, I'm not really sure what your problem is. The following code that I've written works as, I think, intended. If I had to guess what the problem is though, I'd imagine you're not passing around the structure you want to update, but instead copying it over. Hope this helps.

Code:

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

#define CAR_PARK_SIZE 10
typedef struct {
    char *buffer[CAR_PARK_SIZE];
    char *arrival_time[CAR_PARK_SIZE];
    int  keep_running;
    int  size;
 int  index;
 } CarStorage;

typedef struct {
 CarStorage parks;
 CarStorage queue;
 int busy;
 } CarPark;

pthread_mutex_t mutex;

void *addCar( void *arg)
{
 CarPark *_cp = arg;
 pthread_mutex_lock(&mutex);

sleep(1);
 printf("Trying to increase size\n");
 _cp->parks.size = _cp->parks.size+1;
printf("new size: %d\n", _cp->parks.size);
 pthread_mutex_unlock(&mutex);
}
#define NUM_THREADS 5
int main()
{
        pthread_t threads[NUM_THREADS];
        int rc;
        long t;
        CarPark c;
        c.parks.size = 0;
        pthread_mutex_init(&mutex, NULL);
        for(t=0; t<NUM_THREADS; t++)
        {
                printf("In main: creating thread %ld\n", t);
                rc = pthread_create(&threads[t], NULL, addCar, (void *)&c);
                if (rc)
                {
                        printf("ERROR; return code from pthread_create() is %d\n", rc);
                        exit(-1);
                }
        }
        pthread_exit(NULL);
        return 0;
}
于 2009-09-04T03:27:34.170 回答