C++ 信号量、sem_post和分段错误
调用C++ 信号量、sem_post和分段错误,c++,multithreading,segmentation-fault,semaphore,C++,Multithreading,Segmentation Fault,Semaphore,调用sem\u post()函数时,我遇到了分段错误问题。这是我的代码(正在检查超时): 父线程: void * Client::Init(void * args) { char * clientIP; // clientIP = ... IP from other program part int * timeoutSemaphoreValue; sem_unlink(clientIP); // always new semaphore sem_t
sem\u post()
函数时,我遇到了分段错误问题。这是我的代码(正在检查超时):
父线程:
void * Client::Init(void * args)
{
char * clientIP;
// clientIP = ... IP from other program part
int * timeoutSemaphoreValue;
sem_unlink(clientIP); // always new semaphore
sem_t * semaphore;
semaphore = sem_open(clientIP, O_CREAT | O_EXCL, O_RDWR, 0);
if(errno == EEXIST)
cout << "Problem z synchronizacją wątków (semafory) - nieobsłużone." << endl;
pthread_create(&clientStatesThread, NULL, VOID_T Client::ClientStatesLoop, clientIP);
while(status > 0)
{
sem_getvalue(semaphore, timeoutSemaphoreValue);
if(*(client->timeoutSemaphoreValue) == 1)
throw SocketException(0x01, "Timeout.", errno)
}
}
以下是GDB日志:
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
TCP Server v.2.0
[New Thread 0x7ffff6fd6700 (LWP 2425)] // another thread
[New Thread 0x7ffff67d5700 (LWP 2426)] // another thread
Połączono.
[New Thread 0x7ffff5fd4700 (LWP 2529)] // parent thread
[New Thread 0x7ffff57d3700 (LWP 2530)] // child thread
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff57d3700 (LWP 2530)]
0x00007ffff7bcb1c0 in sem_post () from /lib/x86_64-linux-gnu/libpthread.so.0
当我调用
sem\u post()
时,这个segfault的原因是什么?也许我应该在子线程中使用除sem\u open()
以外的其他方法按名称semafore打开?问题已解决。我没有权限。我以root用户身份运行程序,一切正常。:)
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
TCP Server v.2.0
[New Thread 0x7ffff6fd6700 (LWP 2425)] // another thread
[New Thread 0x7ffff67d5700 (LWP 2426)] // another thread
Połączono.
[New Thread 0x7ffff5fd4700 (LWP 2529)] // parent thread
[New Thread 0x7ffff57d3700 (LWP 2530)] // child thread
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff57d3700 (LWP 2530)]
0x00007ffff7bcb1c0 in sem_post () from /lib/x86_64-linux-gnu/libpthread.so.0