How to implement pthread in beaglebone black PRU

Aim: pthread c language code execute in PRU on BeagleBone Black Board.

I refer https://beagleboard.org/static/prucookbook/ in that in run neo LED ws28212b thats work very well.

ISSUE:
then in my next step as per my application, I am required to use thread. but in PRU I understand that that does not use GCC compiler it uses clpru.

so the clpru compiler doesn’t have pthread.h.

CODE
// C program to show thread functions

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include "LED.pru0_lib.h"
void* func1(void* arg)
{
	int i1;
	pthread_detach(pthread_self());
	while(1)
	{
		printf("\nthread1 i1=%d, \t1",i1);
		i1++;
	}

	// exit the current thread
	pthread_exit(NULL);
}
void* func2(void* arg)
{
	int i2;
	pthread_detach(pthread_self());
	while(1)
	{
		printf("\nthread2 i2=%d, \t\t2",i2);
		//hear i want to implement my neo_led code.
		i2++;
	}

	// exit the current thread
	pthread_exit(NULL);
}


void fun()
{
	pthread_t ptid1, ptid2, ptid3, ptid4;

	// Creating a new thread
	pthread_create(&ptid1, NULL, &func1, NULL);
	pthread_create(&ptid2, NULL, &func2, NULL);
	
	printf("This line may be printed"
		" before thread terminates\n");

	// Waiting for the created thread to terminate
	pthread_join(ptid1, NULL);
	pthread_join(ptid2, NULL);

	printf("This line will be printed"
		" after thread ends\n");

	pthread_exit(NULL);
}
// Driver code
int main()
{
	fun();
	return 0;
}

ERROR:
debian@beaglebone:~$ source neo_setup.sh
TARGET=basicThread_led.pru0
Black Found
P9_29

Current mode for P9_29 is:     pruout


Current mode for P9_29 is:     pruout

debian@beaglebone:~$ make
/var/lib/cloud9/common/Makefile:27: MODEL=TI_AM335x_BeagleBone_Black,TARGET=basicThread_led.pru0,COMMON=/var/lib/cloud9/common
/var/lib/cloud9/common/Makefile:101: GEN_DIR=/tmp/cloud9-examples,CHIP=am335x,PROC=pru,PRUN=0,PRU_DIR=/dev/remoteproc/pruss-core0,EXE=.out
-    Stopping PRU 0
/bin/sh: 1: echo: echo: I/O error
Cannot stop 0
CC      basicThread_led.pru0.c
"basicThread_led.pru0.c", line 3: fatal error #1965: cannot open source file "pthread.h"
1 catastrophic error detected in the compilation of "basicThread_led.pru0.c".
Compilation terminated.
Compilation failure

How I use pthread functionality in PRU.

Thank you I appreciate your response.

https://beagleboard.org/static/librobotcontrol/group__pthread.html
Check if using librobotcontrol helps you with what you need.

1 Like

Thank you for your response, I tried but the Robot Control library use GCC compiler, In my case, I use clpru compiler. I need help regarding pthread in clpru compiler.

Refer: PRU Options (Using the GNU Compiler Collection (GCC))
gcc officially supports PRU so I am not sure what you mean by

but in PRU I understand that that does not use GCC compiler it uses clpru.

Is there any other reason you want to use clpru over gcc?

If you are insisting on using clpru then read PRUOptimizingC/C++Compiler and see if you find something useful in it.

1 Like

Thank you, I use clpru for my rgb led. and gcc for pthread. I read Make file but i am implement gcc over clpru.

I wouldn’t use pthreads or printf on the PRUs. Those are both really heavyweight libraries for something as small as a PRU.

The PRUs are 8K local RAM and 200MHz. They’re much more like a small ARM Cortex M0 than anything else.

If you really want “threads” to control your tasks, I’d use a cooperative multitasking library like Protothreads Protothreads - Lightweight, Stackless Threads in C

2 Likes