Today I will talk about the use of multithreading under Linux .
Multithreading was used in the previous serial communication and network communication . Today, I will talk about this multithreading.
Many times you will encounter some blocking functions , such as the select() function (monitoring whether a certain identifier has changed (the previous serial port or network communication identifier)), or the accept() function (waiting for the client's connection), These functions will wait until it detects a change or the client is connected, and then exits, and then executes the following tasks in sequence.
If single-task execution is adopted , such blocking will seriously affect the execution of other tasks , so multitasking is required at this time (ie, "simultaneously" execute many modular programs without delaying each other). The author here is talking about the use of multithreading for multitasking .
Note: When compiling, if you use multithreading, add -lpthread,
arm-linux-gnueabihf-gcc -g -c Mythread.c -o Mythread.o -lpthread
Thread creation function int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg );
Parameter 1: Thread ID
Parameter 2: Thread attribute, default NULL
Parameter 3: The address where the thread starts, that is, the entry address of the function to be executed.
Parameter 4: If the above function requires parameters, put the parameters into the structure and pass in the address as arg .
If the return value is not equal to 0 , the creation fails , otherwise the creation succeeds .
End of thread: pthread_join(*pthread, NULL);
ID of the thread itself: pthread_self();
Create thread
int MyThreadStart(void*(*start_rountine)(void*),pthread_t *thread,void *arg)
{
memset(thread,0x00,sizeof(pthread_t));
int TempReturnValue;
TempReturnValue = pthread_create(thread,NULL,start_rountine,arg); //创建线程函数
if(TempReturnValue!=0)
{
printf("thread created failed!");
}
else
{
int id = pthread_self();
printf("thread id = %d",id);
}
return TempReturnValue;
}
End thread
void MyThreadStop(pthread_t *pthread)
{
if(*pthread!=0)
{
pthread_join(*pthread, NULL);
printf("thread is over!");
}
}
Thread locking and unlocking
In order to prevent multiple threads from using a resource at the same time, such as operating a memory space at the same time, an error will be caused. Therefore, when the thread uses the resource, it must be locked , and then unlocked after the use is completed .
pthread_mutex_t CCURec_mutex_t = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock(&CCURec_mutex_t );
DeQueue(TempPlatformRecQueue,&TempData);
pthread_mutex_unlock(&CCURec_mutex_t );
Alarm function
Equivalent to the timer function , signal() indicates the function to be triggered when the time is up , and alarm() indicates the timing time .
void handler()
{
CCU_To_VST_IntersectionData();
alarm(T);
}
void TimerFunc()
{
signal(SIGALRM, handler); //注册定时器处理函数
alarm(T); //闹钟函数
}
Actually call three thread functions to run it:
int temp = MyThreadStart(pthread_NetRecFun_Platform,&PlatformRec_Id,&ArgPlatformRec);
if(temp == 0)
{
printf("PlatformRecfun threadFunc successfully!\n");
}
temp = MyThreadStart(pthread_NetSendFun_Platform,&PlatformSend_Id,&ArgPlatformSend);
if(temp == 0)
{
printf("PlatformSendfun threadFunc successfully!\n");
}
temp = MyThreadStart(pthread_NetProcessFun_Platform,&PlatformProcess_Id,&ArgPlatformProcess);
if(temp == 0)
{
printf("PlatformProcessfun threadFunc successfully!\n");
}
void *pthread_NetRecFun_Platform(void *arg); //socket通信 接收线程函数
void *pthread_NetSendFun_Platform(void *arg); //socket通信 发送线程函数
void *pthread_NetProcessFun_Platform(void *arg); //socket通信 解析线程函数
Previous: C language programming under Linux (4): TCP/IP network communication