undefided reference to 'sem_init' 等信号量问题

解决办法:在编译时加上 -pthread,

eg: gcc -pthread sem.c -o sem

 

 

 

#include<stdio.h>

#include<errno.h>
#include<semaphore.h>

static int shared = 0;
static sem_t sharedsem;

int initshared(int val)
{
    if(sem_init(&sharedsem,0,1) == -1)
  return -1;
 shared = val;

 return 0;
}

int getshared(int *sval)
{
 while(sem_wait(&sharedsem) == -1)
  if(errno != EINTR)
   return -1;

  *sval = shared;

  return sem_post(&sharedsem);
}

int incshared()
{
 while(sem_wait(&sharedsem) == -1)
  if(errno != EINTR)
   return -1;

  shared++;
  return sem_post(&sharedsem);
}
int main()
{
   int sval;
    initshared(1);
 getshared(&sval);
 printf("sval is %d\n",sval);

 incshared();
 printf("shared is %d\n",shared);

 return 0;
}

你可能感兴趣的:(gcc,reference)