LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1041|回复: 1

linux多线程编程问题:

[复制链接]
发表于 2005-2-19 20:42:38 | 显示全部楼层 |阅读模式
linux多线程编程问题:
/***************************************************************************
                          main.c  -  description
                             -------------------
    begin                : 四  2月 10 19:30:17 CST 2005
    copyright            : (C) 2005 by
    email                :
***************************************************************************/

/***************************************************************************
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
***************************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
#include <semaphore.h>
#define BUFSIZE 4
#define NUMBER 8

int sum_of_number=0;
sem_t write_res_number;
sem_t read_res_number;

//cycle queue
struct recycle_buffer
{
  int buffer[BUFSIZE];
  int head,tail;
}re_buf;

pthread_mutex_t buffer_mutex=PTHREAD_MUTEX_INITIALIZER;

//extern int sem_init (sem_t *__sem, int __pshared, unsigned int __value) __THROW;


static void *producer(void *arg)
{
  int i;
  for(i=0;i<NUMBER;i++)
  {
    sem_wait(&write_res_number);
    pthread_mutex_lock(&buffer_mutex);
    re_buf.buffer[re_buf.tail]=i;
    re_buf.tail=(re_buf.tail++)%BUFSIZE;
    printf("producer %d write %d.\n",pthread_self(),i);
    pthread_mutex_unlock(&buffer_mutex);
    sem_post(&read_res_number);
  }
  return NULL;
}

static void *consumer(void *arg)
{
  int i,num;

  for(i=0;i<NUMBER;i++)
  {
    sem_wait(&read_res_number);
    pthread_mutex_lock(&buffer_mutex);
    num=re_buf.buffer[re_buf.head];
    re_buf.head=(re_buf.head+1)%BUFSIZE;
    printf("consumer %d read %d.\n",pthread_self(),num);
    pthtread_mutex_unlock(&buffer_mutex);
    sum_of_number+=num;
    sem_post(&write_res_number);
   }
   return NULL;
}

void main(int argc, char *argv[])
{
  pthread_t p_tid,c_tid;
  int i;

  re_buf.head = 0;
  re_buf.tail = 0;
  for(i=0;i<BUFSIZE;i++)
    re_buf.buffer = 0;
  sem_init(&write_res_number,0,BUFSIZE);
  sem_init(&read_res_number,0,0);
  pthread_create(&p_tid,NULL,producer,NULL);
  pthread_create(&c_tid,NULL,consumer,NULL);
  pthread_join(p_tid,NULL);
  pthread_join(c_tid,NULL);
  printf("The sum of number is %d\n",sum_of_number);
}
  

  
-------------------------
我在kdevelop下运行时候,出现这个错误:
lin15_2.o(.text+0x213):/root/myWorks/lin15_2/src/lin15_2.c:90: undefined reference to `sem_init'
lin15_2.o(.text+0x228):/root/myWorks/lin15_2/src/lin15_2.c:91: undefined reference to `pthread_create'
lin15_2.o(.text+0x23d):/root/myWorks/lin15_2/src/lin15_2.c:92: undefined reference to `pthread_create'
lin15_2.o(.text+0x24d):/root/myWorks/lin15_2/src/lin15_2.c:93: undefined reference to `pthread_join'
lin15_2.o(.text+0x25d):/root/myWorks/lin15_2/src/lin15_2.c:94: undefined reference to `pthread_join'


而我这些文件已经包括了啊.
发表于 2005-2-22 17:47:03 | 显示全部楼层
请注意gcc编译时带没带参数"-lphtread"
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表