Product SiteDocumentation Site

4.4. Abusing libtcr for cooperative task switching

For sake of completeness libtcr also exposes the tc_sched_yield() function. A call to that function places the current tc_thread at the tail of the queue and calls tc_scheduler(). If there were other tc_threads waiting in the queue, those will get a chance to run before the current tc_thread continues.
#include <tc/threaded_cr.h>

static void func(void *str)
	int i;

	for (i=0; i<100; i++) {
		printf("%d %s\n", i, (char *)str);

static void starter(void *unused)
	struct tc_thread *a, *b;

	a = tc_thread_new(func, "a", "func_a");
	b = tc_thread_new(func, "b", "func_b");


int main()
	tc_run(starter, NULL, "test", 1);
	/* tc_run(starter, NULL, "test", sysconf(_SC_NPROCESSORS_ONLN)); */
	return 0;
When the tc_sched_yield() call is removed from the example, the two threads execute without any parallelism, since tc_run()'s last parameter is 1. This means that this program executes in a single system level thread, i.e. on a single CPU.
Example 2. tc_sched_yield() example