From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,658a01eda59cffe3 X-Google-Attributes: gidfee84,public X-Google-ArrivalTime: 1994-02-14 08:56:18 PST Path: gmd.de!newsserver.jvnc.net!news.edu.tw!news!nctuccca.edu.tw!howland.reston.ans.net!europa.eng.gtefsd.com!news.umbc.edu!eff!news.kei.com!bloom-beacon.mit.edu!paperboy.osf.org!sp From: sp@canal.osf.org (Simon Patience) Newsgroups: comp.unix.osf.osf1 Subject: Re: pthreads Date: 14 Feb 1994 16:56:18 GMT Organization: Open Software Foundation Distribution: world Message-ID: <2joafi$5bb@paperboy.osf.org> References: <2jgi3o$999@winx03.informatik.uni-wuerzburg.de> NNTP-Posting-Host: canal.osf.org 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 #include 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