Linux Advanced Application multithreading programming

1, Why multithreading?

			else if(60<xy.x && xy.x<300 && 240<xy.y && xy.y<300)//Enter LCD detection
			{
				printf("enter LCD checking\n");
				while(1)
				{
					lcd_show_color(RED);
					xy = get_ts_xy();//block
					else if(700<xy.x && xy.x<800 && 0<xy.y && xy.y<50)//return
					{
						printf("return control UI\n");
						xy.x=0;xy.y=0;	
					}
					sleep(1);
					
					lcd_show_color(GREEN);
					xy = get_ts_xy();
					else if(700<xy.x && xy.x<800 && 0<xy.y && xy.y<50)//return
					{
						printf("return control UI\n");
						xy.x=0;xy.y=0;	
					}
					sleep(1);

					xy = get_ts_xy();
					else if(700<xy.x && xy.x<800 && 0<xy.y && xy.y<50)//return
					{
						printf("return control UI\n");
						xy.x=0;xy.y=0;	
					}
					lcd_show_color(BLUE);

					xy = get_ts_xy();
					else if(700<xy.x && xy.x<800 && 0<xy.y && xy.y<50)//return
					{
						printf("return control UI\n");
						xy.x=0;xy.y=0;	
					}
					sleep(1);
					lcd_show_color(WHITE);

					xy = get_ts_xy();
					else if(700<xy.x && xy.x<800 && 0<xy.y && xy.y<50)//return
					{
						printf("return control UI\n");
						xy.x=0;xy.y=0;	
					}
					sleep(1);
					lcd_show_color(BLACK);

					xy = get_ts_xy();
					else if(700<xy.x && xy.x<800 && 0<xy.y && xy.y<50)//return
					{
						printf("return control UI\n");
						xy.x=0;xy.y=0;	
					}
					sleep(1);
				}
				xy.x=0;xy.y=0;
			}

When obtaining the coordinates of the touch screen, if the touch screen does not press, the process of reading the coordinates of the touch screen will be blocked, and the cyclic display of monochrome will not cycle automatically.

========================================================================================================

2, How to solve the problem?

void show_color(void) / / loop to display monochrome child threads
{
while(1)
{
lcd_show_color(RED);
sleep(1);

	lcd_show_color(GREEN);
	sleep(1);

	lcd_show_color(BLUE);
	sleep(1);
	
	lcd_show_color(WHITE);
	sleep(1);
	
	lcd_show_color(BLACK);
	sleep(1);
}

}

main(void) / / main thread
{
...
Else if (60 < XY. X & & XY. X < 300 & & 240 < XY. Y & & XY. Y < 300) / / enter the LCD screen for detection
{
printf("enter LCD checking\n");
Create a sub thread - > loop play monochrome (show_color())
while(1)
{
xy = get_ts_xy();// Blocking, only the main thread is blocked, and the child thread can continue to run
If (700 < XY. X & & XY. X < 800 & & 0 < XY. Y & & XY. Y < 50) / / return
{
printf("return control UI\n");
//Make the child thread show_color() launches and ends the cycle to display monochrome
xy.x=0;xy.y=0;
breakļ¼›// Push out the monochrome display interface and enter the control interface.
}
}
xy.x=0;xy.y=0;
}
}

========================================================================================
3, Thread handler
1. Thread creation
Another thread can be created in one thread. The original thread is usually called the main thread, and the newly created thread is called the sub thread. Both the main thread and the child thread belong to the same process. Resources (global variables, heap, stack, registers, memory, files,...) are shared between the main thread and child threads. If the main thread exits, all child threads created by the main thread will exit.

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
Compile and link with -pthread.
Parameter Description:
pthread_t *thread -- the ID of the created sub thread, the unsigned integer of 32 bits, and the ID is also allocated by the system.
const pthread_attr_t *attr -- the attribute of the thread. It can not be used. It is NULL
void *(*start_routine) (void *) ----- the function executed by the created sub thread
void *arg ---- the parameter passed to the child thread running function
Return value:
On success, pthread_create() returns 0; on error, it returns an error number

2. Thread exit
In one thread, we can let another thread exit.
#include <pthread.h>
int pthread_cancel(pthread_t thread);

Parameters:
pthread_t thread ID of the exit thread.

3. The thread exits itself
#include <pthread.h>
void pthread_exit(void *retval);

4. Get thread ID
#include <pthread.h>
pthread_t pthread_self(void);

5. Thread waiting function
#include <pthread.h>
int pthread_join(pthread_t thread, void **retval);
Call this function in the main thread. Wait for the child thread to exit. If the child thread does not exit, the main thread calling this function will wait.

be careful:
When compiling multithreaded programs, you need to use - lpthread

===================================================================================
4, Solution 2

struct ts_xy xy; // Global variable - shared by the main thread and child threads

void *ts_thread(void *) - get touch screen coordinates
{
while(1)
{
xy = get_ts_xy();// Assign values to global variables
}
}

main(void) / / main thread - use touch screen coordinates
{
Create a child thread ts_thread() - get the coordinates of the touch screen

..............................................................
else if(60<xy.x && xy.x<300 && 240<xy.y && xy.y<300)//Enter LCD detection
{
	printf("enter LCD checking\n");
	while(1)
	{
		lcd_show_color(RED);
		else if(700<xy.x && xy.x<800 && 0<xy.y && xy.y<50)//return
		{
			printf("return control UI\n");
			xy.x=0;xy.y=0;	
		}
		sleep(1);
		lcd_show_color(GREEN);

		else if(700<xy.x && xy.x<800 && 0<xy.y && xy.y<50)//return
		{
			printf("return control UI\n");
			xy.x=0;xy.y=0;	
		}
		sleep(1);

		else if(700<xy.x && xy.x<800 && 0<xy.y && xy.y<50)//return
		{
			printf("return control UI\n");
			xy.x=0;xy.y=0;	
		}
		lcd_show_color(BLUE);

		else if(700<xy.x && xy.x<800 && 0<xy.y && xy.y<50)//return
		{
			printf("return control UI\n");
			xy.x=0;xy.y=0;	
		}
		sleep(1);
		lcd_show_color(WHITE);

		else if(700<xy.x && xy.x<800 && 0<xy.y && xy.y<50)//return
		{
			printf("return control UI\n");
			xy.x=0;xy.y=0;	
		}
		sleep(1);
		lcd_show_color(BLACK);

		else if(700<xy.x && xy.x<800 && 0<xy.y && xy.y<50)//return
		{
			printf("return control UI\n");
			xy.x=0;xy.y=0;	
		}
		sleep(1);
	}
	xy.x=0;xy.y=0;
}

}

How to close the automatic operation program of the test chamber (how to add your own program to make the test chamber run automatically)

1. Embedded Linux (test box startup process)
Power on - > run the boot code (similar to BIOS), also known as bootloader. Usually, use u-boot - > start and load the Linux kernel - > mount rootfs (shell command, Linux configuration, device file, library running Linux application,...) - > run the script / etc / init.d/rcs (or / etc / profile) - -- > to configure Linux system environment variables to start the application

2. How to close the automatic running application of the test box
1) Open the / etc/init.d/rcS file using vi
vi is a text editor that comes with Linux, similar to vim
Note: see P5 for the use of vi

[root@FriendlyARM /]# vi /etc/init.d/rcS

Last of rcS file:

/Bin / Qtopia & ------------- runs a Linux Application in the background
echo " " > /dev/tty1
echo "Starting Qtopia, please waiting..." > /dev/tty1

We need to comment out these three lines.

2) First, enter the i key to change vi from command mode to insert mode
You can modify files only in insert mode. Use # comments to remove the following three lines.

#/bin/qtopia &
#echo " " > /dev/tty1
#echo "Starting Qtopia, please waiting..." > /dev/tty1

I /etc/init.d/rcS [Modified] 87/87 100%

3) Enter the "Esc" key to exit the insertion mode and return to the command mode

#/bin/qtopia &
#echo " " > /dev/tty1
#echo "Starting Qtopia, please waiting..." > /dev/tty1

  • /etc/init.d/rcS [Modified] 87/87 100%

4) Save exit
Enter ": wq"

#/bin/qtopia &
#echo " " > /dev/tty1
#echo "Starting Qtopia, please waiting..." > /dev/tty1

:wq

5) Restart
The development board will only display the startup interface and will not enter the QT interface

#include <stdio.h>
#include <pthread.h>

/****Child thread****/
unsigned int count = 0;
void *show_color(void *arg)
{
	while(1)
	{
		count ++;	
		sleep(1);		
	}
}


/****Main thread****/
int main(void)
{
	pthread_t tid_color;
	int ret;
	ret = pthread_create(&tid_color, NULL, show_color, NULL);
	if(ret < 0)
	{
		perror("color thread create error");
		return -1;
	}
	while(1)
	{
		printf("count =%d\n",count);
		if(count == 10)
			pthread_cancel(tid_color);

	}
	
	return 0;
}

Keywords: C Linux Algorithm Multithreading Embedded system

Added by love_php on Sat, 18 Sep 2021 16:58:47 +0300