Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
From: sp@canal.osf.org (Simon Patience)
Subject: Re: pthreads
Date: 14 Feb 1994 16:56:18 GMT	[thread overview]
Message-ID: <2joafi$5bb@paperboy.osf.org> (raw)
In-Reply-To: 2jgi3o$999@winx03.informatik.uni-wuerzburg.de

In article <2jgi3o$999@winx03.informatik.uni-wuerzburg.de>, klingspo@informatik.uni-wuerzburg.de (Markus Klingspor) writes:
> I'm looking for a working example how to create at least one thread using 
> pthread_create.

This program was written to 1003.4a Draft 4. When you run it, give it the 
number of threads you want to create. On OSF/1, remember to link using
	-lpthreads -lmach -lc_r
in that order.

Enjoy
Simon.

---cut here---cut here---cut here---cut here---cut here---cut here---cut here---
#include	<pthread.h>
#include	<stdio.h>

int	count	= 0;
pthread_mutex_t	lock;
pthread_cond_t	done;

typedef struct slave_struct {
	char	name[10];
	int	count;
} *slave_info_t;

void *
slave(void *arg)
{
	slave_info_t info;
	int	i;

	info = (slave_info_t)arg;
	for (i = 0; i < info->count; i++)
		pthread_yield();
	pthread_mutex_lock(&lock);
	count--;
	printf("%s finished %d cycles\n", info->name, info->count);
	free(info);
	pthread_mutex_unlock(&lock);
	pthread_cond_signal(&done);
}

void *
master(nslaves)
int	nslaves;
{
	int	i;
	pthread_t	new_thread;
	slave_info_t	slaveinfo;

	for (i = 1; i <= nslaves; i++) {

		slaveinfo = (slave_info_t)malloc(sizeof(struct slave_struct));
		sprintf(slaveinfo->name, "slave%d", i);
		slaveinfo->count = random()%1000;
		pthread_mutex_lock(&lock);
		count++;
		pthread_create(&new_thread, pthread_attr_default,
						slave, (void *)slaveinfo);
		pthread_detach(&new_thread);
		pthread_mutex_unlock(&lock);
	}
	pthread_yield();
	pthread_mutex_lock(&lock);
	while (count != 0)
		pthread_cond_wait(&done, &lock);
	pthread_mutex_unlock(&lock);
	printf("All %d slaves have finished\n", nslaves);
	return((void *)nslaves);
}

void
main(argc, argv)
int	argc;
char	**argv;
{
	pthread_t	new_thread;
	void		*status;
	int		ntimes;

	if (argc != 2) {
		printf("usage: master loop_count\n");
		exit(1);
	}
	pthread_mutex_init(&lock, pthread_mutexattr_default);
	pthread_cond_init(&done, pthread_condattr_default);
	ntimes = atoi(argv[1]);
	pthread_create(&new_thread, pthread_attr_default, master, (void *)ntimes);
	if (pthread_join(new_thread, &status) != 0) {
		perror("main_join");
		exit(1);
	}
	printf("master exited with status = %d\n", status);
	pthread_detach(&new_thread);
	pthread_exit(0);
}

---cut here---cut here---cut here---cut here---cut here---cut here---cut here---

-- 
  Simon Patience				Phone: (617) 621-7376
  Open Software Foundation			FAX:   (617) 621-8696
  1 Cambridge Center				Email: sp@osf.org
  Cambridge, MA 02142				       uunet!osf!sp





  reply	other threads:[~1994-02-14 16:56 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1994-02-11 18:17 pthreads Markus Klingspor
1994-02-14 16:56 ` Simon Patience [this message]
  -- strict thread matches above, loose matches on Subject: below --
2000-04-26  4:00 pthreads Harish
2000-04-26  4:00 ` pthreads Fred Zwarts
2000-04-26  4:00   ` pthreads Harish
2000-04-26  4:00     ` pthreads Steffen Neumann
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox