字节对齐问题引起的sem_wait 段错误

今天写代码用到了信号量,使用sem_timedwait();时报出段错误如下:

0x00007ffff6109418 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
54 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0  0x00007ffff6109418 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
#1  0x00007ffff610b01a in __GI_abort () at abort.c:89
#2  0x00007ffff614b72a in __libc_message (do_abort=do_abort@entry=1, fmt=fmt@entry=0x7ffff6262aa9 "%s")
    at ../sysdeps/posix/libc_fatal.c:175
#3  0x00007ffff614b74e in __GI___libc_fatal (
    message=message@entry=0x7ffff7bccb40 "The futex facility returned an unexpected error code.")
    at ../sysdeps/posix/libc_fatal.c:185
#4  0x00007ffff7bc9ab5 in futex_fatal_error () at ../sysdeps/nptl/futex-internal.h:200
#5  futex_abstimed_wait_cancelable (private=0, abstime=0x7fffde7fbe80, expected=0, futex_word=0x619b2d)
    at ../sysdeps/unix/sysv/linux/futex-internal.h:223
#6  do_futex_wait (sem=sem@entry=0x619b2d, abstime=abstime@entry=0x7fffde7fbe80) at sem_waitcommon.c:111
#7  0x00007ffff7bc9b1f in __new_sem_wait_slow (sem=0x619b2d, abstime=0x7fffde7fbe80)
    at sem_waitcommon.c:181
#8  0x00007ffff7bc9bd2 in sem_timedwait (sem=, abstime=)
    at sem_timedwait.c:36
#9  0x00007ffff6995fc2 in V2v64PassthroughImpl::ReliableSendThreadProc (pParam=0x619ae0)
    at /mnt/hgfs/share/PassThroughCode3/trunk/src/vv_service/vvs_PassthroughImpl.cpp:354
#10 0x00007ffff7bc16fa in start_thread (arg=0x7fffde7fc700) at pthread_create.c:333

#11 0x00007ffff61dab5d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109

下面贴出初始化的代码:

V2v64PassthroughImpl::V2v64PassthroughImpl(void)
{
m_pV2v64PassthroughCallback = NULL;
m_bReliableSendThreadStart = false;
m_nTransType = -1;
m_nCurRecvGroupIndex = 0;
m_nCurRecvGroupPkts = 0;
m_bCurRecvGroupLostPkts = true;
m_bCurMsgDirty = false;
#ifdef _MSC_VER
InitializeCriticalSection(&m_csReliableSend);
m_hReliableSendEvent = NULL;
m_hReliableSendThread = NULL;
#else
    bIsTreadEnd = false; 
m_threadId = 0;
pthread_mutex_init(&m_csReliableMutex, NULL);
sem_init(&m_sem, 0, 0);
#endif

}

void* V2v64PassthroughImpl::ReliableSendThreadProc(void* pParam)

{

   ....

struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_nsec += 500000000;
int s = sem_timedwait(&pThis->m_sem, &ts); //段错误了,此时sem值为零,还没有post

   ....

}

类中定义:

class V2v64PassthroughImpl
{
     ...
pthread_t m_threadId;
pthread_mutex_t m_csReliableMutex;

sem_t m_sem;

     ...

}

头文件中没有一字节对齐,但是将头文件加上#programe pack(4) 就可以了,不清楚为什么。

 

#programe pack(4)

class V2v64PassthroughImpl

 

{
     ...
pthread_t m_threadId;
pthread_mutex_t m_csReliableMutex;

 

sem_t m_sem;

     ...

}

 

你可能感兴趣的:(字节对齐问题引起的sem_wait 段错误)