shithub: brokentoys

ref: 5a0e28438fe4c0f8cc55430c388c2016cc185cbf
dir: /t.c/

View raw version
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <thread.h>

Channel *spamchan;

void
freethread(void *)
{
	char *v = nil;

	threadsetname("freethread");
Loop:
	recv(spamchan, &v);
	if(v == nil){
		print("nothing to free\n");
		threadexitsall(0);
	}
	print("freeing %s\n", v);
	free(v);
	goto Loop;
}

void
spammer(void*)
{
	int i;
	char *s;

	threadsetname("spammer");
	for(i = 0; i < 10; ++i){
		s = smprint("%d", i);
		send(spamchan, &s);
	}
	send(spamchan, nil);
}

void
threadmain(int argc, char *argv[])
{
	print("acid -l thread %d\n", getpid());
	threadsetname("main");
	spamchan = chancreate(sizeof(char*), 0);
	threadcreate(spammer, nil, 8192);
	threadcreate(freethread, nil, 8192);
	yield();
}