简单多线程,互斥锁,信号量例子

#include
#include
#include
#include
#include
static pthread_mutex_t g_tNetDbgSendMutex  = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t  g_tNetDbgSendConVar = PTHREAD_COND_INITIALIZER;


static pthread_t g_tSendTreadID;
static pthread_t g_tRecvTreadID;
static char s_sGetStdio[128];


static void *PthreadGetStdio(void *pVoid)
{
char sStr[128];


while(1)
{
scanf("%s",sStr);
if (sStr[0] == '0')
{
strcpy(s_sGetStdio, sStr);


#if 1
pthread_mutex_lock(&g_tNetDbgSendMutex);
pthread_cond_signal(&g_tNetDbgSendConVar);
pthread_mutex_unlock(&g_tNetDbgSendMutex);
#endif

}
}
}
static void *PthreadPrintf(void *pVoid)
{
char sStr[128];


while(1)
{
#if 1
/* 平时休眠 */
pthread_mutex_lock(&g_tNetDbgSendMutex);
pthread_cond_wait(&g_tNetDbgSendConVar, &g_tNetDbgSendMutex);
pthread_mutex_unlock(&g_tNetDbgSendMutex);
#endif
printf("PthreadPrintf : %s\n",s_sGetStdio);

}


}




int main(void)
{




  /* 创建 发送线程: 它用来获取键盘的输入,然后发给另外一下线程*/
  pthread_create(&g_tSendTreadID, NULL, PthreadGetStdio, NULL);  
  
  /* 创建接收线程: 用来接收信息,将得到的信息打印出来 */
  pthread_create(&g_tRecvTreadID, NULL, PthreadPrintf, NULL);   


  while(1);


  return 0;
}

你可能感兴趣的:(多线程)