简体   繁体   English

C-Pthreads互斥锁和普遍头痛

[英]C - Pthreads mutex and general headaches

Hey guys I was wondering if someone could provide a little help. 大家好,我想知道是否有人可以提供一些帮助。

I've been trying to teach myself pthreads and with that, mutex locks to get threads running together and using the same structure, whilst not reading and writing to bad data. 我一直在尝试自学pthread,并使用互斥锁来使线程一起运行并使用相同的结构,同时又不对不良数据进行读写。

My problem at the moment is, 我目前的问题是

From my thread function, if i call a helper function that might look something similar to the following: 从我的线程函数中,如果我调用了一个类似于以下内容的助手函数:

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

This above helper method doesn't seem to "update" the structure. 上面的助手方法似乎没有“更新”结构。

But if I run the same code in the thread function, the exact same 4 lines, than this seems to work. 但是,如果我在线程函数中运行相同的代码,则完全相同的4行似乎比这行得通。

What am I doing wrong? 我究竟做错了什么? Or how do I fix this? 或者我该如何解决? If anyone could provide some reading as well that would be perfect. 如果有人能提供一些阅读材料,那将是完美的。

EDIT: Sorry guys that was a typo in my code. 编辑:对不起,这是我代码中的错字。

Here is the actual code I'm using for the structure. 这是我用于该结构的实际代码。

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

If the same lines in addCar are in the thread function, it will increase the size, if its in this helper function, it won't. 如果addCar中的相同行在线程函数中,它将增加大小,如果在此辅助函数中,则不会增加大小。

Here is the calling code 这是调用代码

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

}

---- Snipped because it no longer applies and didn't work anyway ---- ----因为它不再适用而无法正常工作而被剪断----

---- Snipped some more because it no longer applies and didn't work anyway ---- -删除了一些,因为它不再适用并且仍然无法正常工作-

And here is your error: 这是您的错误:

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

That &_cp is passing in the address of _cp , which is a pointer to _cp . 也就是说&_cp被传递的地址 _cp ,这是一个指针_cp but _cp is already a pointer, so you're passing in a pointer to a pointer . 但是_cp已经是一个指针,因此您要将一个指针传递给了指针 Either change &_cp to regular _cp , or change void addCar(char *car, void *arg) to void addCar(char *car, void **arg) (and edit addCar() accordingly). 可以将&_cp更改为常规_cp ,或者将void addCar(char *car, void *arg)更改为void addCar(char *car, void **arg) (并相应地编辑addCar() )。 Either one should work, but I'd recommend the first one, as it's easier. 任一个都可以,但是我建议第一个,因为它更容易。

What you're doing in addCar with the locking is fine. 您在具有锁定功能的addCar中所做的事情很好。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM