2014年4月26日土曜日

開発環境

Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の12章(スレッド)、ビールマグネット(p.509)を解いてみる。

その他参考書籍

ビールマグネット(p.509)

コード(BBEdit, Emacs)

sample509.c

#include <stdio.h>
#include <pthread.h>

#include "drink_lots.h"
#include "error.h"

int main(int argc, char *argv[])
{
  pthread_t threads[20];
  int t;

  printf("壁にはビールが%i本\n%i本のビール\n", beers, beers);
  for (t = 0; t < 20; ++t)
    pthread_create(&threads[t], NULL, drink_lots, NULL);
  void *result;
  for (t = 0; t < 20; ++t)
    pthread_join(threads[t], &result);
  
  /* 各スレッドがbeersに同時にアクセスできるで、0本になるとはかぎらない。 */
  printf("現在、壁にはビールが%i本あります\n", beers);
  return (0);
}

Makefile

all: sample509

sample509: sample509.c error.o drink_lots.o
 cc -g -o sample509 sample509.c error.o drink_lots.o

drink_lots.o: drink_lots.c
 cc -c -o drink_lots.o drink_lots.c

error.o: error.c
 cc -c -o error.o error.c

clean:
 rm -rf sample509

入出力結果(Terminal)

$ make
cc -c -o drink_lots.o drink_lots.c
cc -g -o sample509 sample509.c error.o drink_lots.o
$ ./sample509
壁にはビールが2000000本
2000000本のビール
現在、壁にはビールが1389616本あります
$  ./sample509
壁にはビールが2000000本
2000000本のビール
現在、壁にはビールが1296461本あります
$  ./sample509
壁にはビールが2000000本
2000000本のビール
現在、壁にはビールが1357809本あります
$  ./sample509
壁にはビールが2000000本
2000000本のビール
現在、壁にはビールが1419295本あります
$  ./sample509
壁にはビールが2000000本
2000000本のビール
現在、壁にはビールが1294705本あります
$

0 コメント:

コメントを投稿