C ++正确使用互斥锁 [英] c++ correct use of mutex

查看:429
本文介绍了C ++正确使用互斥锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多线程项目,并且我已使用--tool = helgrind通过valgrind运行了该项目,它显示了一些错误.我在那里过分地使用互斥锁,我在网上发现如何使用它,您能告诉我怎么了吗?

i have multithreaded project and i've run it through valgrind with --tool=helgrind and it showed me few errors. I'm using mutex there exaxtly how i found on the net how to use it, can you please show me whats wrong?

#include <iostream>
#include <pthread.h>

#define MAX_THREADS     100
#define MAX_SESSIONS    100

static pthread_mutex_t  M_CREATE_SESSION_LOCK= PTHREAD_MUTEX_INITIALIZER;


.....



void connection::proccess(threadVarsType &THREAD) {
    ....

    pthread_mutex_lock(&M_CREATE_SESSION_LOCK);

    unsigned int ii;
    for (ii=0; ii<MAX_SESSIONS; ii++) {
        if (SESSION[ii]==NULL) {
            break;
        }
    }
    if (ii==MAX_SESSIONS-1) {
        ....
        pthread_mutex_unlock(&M_CREATE_SESSION_LOCK);                       // unlock session mutex
        ....
        return;
    } else {
        ....
        pthread_mutex_unlock(&M_CREATE_SESSION_LOCK);                       // unlock session mutex
        ....
    }

    ....
}

和错误消息:

==4985== Thread #1's call to pthread_mutex_lock failed
==4985==    with error code 22 (EINVAL: Invalid argument)
    ....
==4985== Thread #1 unlocked an invalid lock at 0x4E7B40
==4985==    at 0x32CD8: pthread_mutex_unlock (hg_intercepts.c:610)
    ....
==4985== Thread #1's call to pthread_mutex_unlock failed
==4985==    with error code 22 (EINVAL: Invalid argument)
    ....
==4985== Thread #1's call to pthread_mutex_lock failed
==4985==    with error code 22 (EINVAL: Invalid argument)
    ....
==4985== Thread #1 unlocked an invalid lock at 0x4E7B40
==4985==    at 0x32CD8: pthread_mutex_unlock (hg_intercepts.c:610)
    ....
==4985== Thread #1's call to pthread_mutex_unlock failed
==4985==    with error code 22 (EINVAL: Invalid argument)

推荐答案

首先,始终检查函数调用的返回值.如果pthread调用失败,则最好只调用abort(),如果启用了该功能,它将进行核心转储,或者如果正在运行,则将其放入调试器.

First, always check the return values of your function calls. If a pthread call fails, it's a good choice to just call abort() which will core-dump if you have that enabled or drop into the debugger if you are running with one.

pthread函数调用确实应该永远不会失败,这意味着程序有严重问题.在C或C ++程序中,通常导致神秘故障的原因是内存损坏.在正常模式下使用valgrind进行检查.

The pthread function calls really should never fail, which means that something is seriously wrong with your program. In a C or C++ program something that commonly causes mysterious failures is memory corruption. Use valgrind in its normal modes to check for that.

可能导致pthread调用失败的另一件事是不使用-pthread进行编译.如果使用GCC,则应使用之类的命令使用gcc进行编译和链接.这将链接pthread库,并将设置一些预处理程序定义,这可能对系统的头文件很重要.

Another thing that can cause pthread calls to fail is to not compile using -pthread. If using GCC you should compile and link using gcc with a command like gcc -pthread. That will link the pthread library and it will set some preprocessor defines that may be important for your system's header files.

某些系统将成功编译并链接使用pthread调用的程序,而无需将其链接到pthread库.这样做是为了使程序或库可以成为线程安全的,而无需实际使用线程.除非链接了真正的pthread库,否则线程调用将链接到虚拟函数.这可能导致某些函数调用失败.

Some systems will successfully compile and link a program that is using pthread calls without linking it to the pthread libraries. This is done so that a program or library can be made thread-safe without actually using threads. The thread calls will be linked to dummy functions unless the real pthread library is linked. That can lead to some function calls failing.

因此,请确保使用正确的编译器选项进行构建以包含pthread库.

So make sure you are building with the correct compiler options to include the pthread libraries.

另一个可能的原因是,如果您正在构建一些破旧的半混合操作系统,该操作系统从Linux 2.4开始并在某个时候升级到Linux 2.6 NPTL(我曾经做过类似的事情).如果尝试使用定义为PTHREAD_MUTEX_INITIALIZER的过时旧文件或类型为pthread_mutex_t的大小错误的旧头文件进行编译,则可能会导致问题.

Another possible cause is if you are building on some whacked-out half-and-half hybrid OS where it started as Linux 2.4 and got upgraded to Linux 2.6 NPTL at some point (I worked on something like this once). If you are attempting to compile against old header files with an outdated definition of PTHREAD_MUTEX_INITIALIZER or the wrong size for the type of pthread_mutex_t then that could cause the problem.

这篇关于C ++正确使用互斥锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆