Function description of C language multithreading pthread library

  • 2020-05-19 05:20:09
  • OfStack

Thread-related operation instructions

1 pthread_t

pthread_t in the header file/usr/include/bits/pthreadtypes h defined in:

typedef unsigned long int pthread_t;

It is the identifier of one thread.

2 pthread_create

The function pthread_create is used to create a thread. Its prototype is:

extern int pthread_create __P ((pthread_t *__thread, __const pthread_attr_t *__attr,

S 30en *(* s 31en_routine) (void *), void * s 35en);

The first parameter is a pointer to the thread identifier, the second parameter is used to set thread properties, the third parameter is the starting address of the thread running function, and the last parameter is the parameter of the running function. Here, our function thread needs no arguments, so the last argument is set to a null pointer. The second parameter we will also set to null pointer, which will generate the default property of the thread. Setting and modifying the thread properties will be covered in the next section. When the thread is created successfully, the function returns 0. If it is not 0, the thread creation fails. The common error return codes are EAGAIN and EINVAL. The latter indicates that the thread property value represented by the second parameter is invalid. After the thread is created successfully, the newly created thread runs the function determined by parameters 3 and 4, and the original thread continues to run the next line of code.

3 pthread_join pthread_exit

The function pthread_join is used to wait for the end of a thread. The function prototype is:

extern int pthread_join __P ((pthread_t __th, void **__thread_return));

The first parameter is the waiting thread identifier, and the second parameter is a user-defined pointer that can be used to store the return value of the waiting thread. This function is a function that is blocked by one thread. The calling function will wait until the waiting thread ends. When the function returns, the resource of the waiting thread is retrieved. There are two ways for a thread to end. One is like our example above. When the function ends, the calling thread ends. Another way is through the function pthread_exit.

Its function prototype is:

extern void pthread_exit __P ((void *__retval)) __attribute__ ((__noreturn__));

The only parameter of 1 is the return code of the function. As long as the second parameter thread_return in pthread_join is not NULL, this value will be passed to thread_return.

In this section, we've written one of the simplest threads and learned about the three most commonly used functions, pthread_create,pthread_join, and pthread_exit. Below, we'll learn about some of the most common properties of threads and how to set them.

Mutex correlation

A mutex is used to ensure that only one thread is executing one piece of code at a time.

1 pthread_mutex_init

The function pthread_mutex_init is used to generate a mutex. The NULL parameter indicates the use of default properties. If you need to declare a mutex for a specific property, the function pthread_mutexattr_init.pthread_mutexattr_setpshared and the function pthread_mutexattr_settype are used to set the mutex property. The first function sets the property pshared, which has two values, PTHREAD_PROCESS_PRIVATE and PTHREAD_PROCESS_SHARED. The former is used to synchronize threads in different processes, while the latter is used to synchronize different threads in the same process. In the above example, we use the default property PTHREAD_PROCESS_PRIVATE. The latter is used to set the type of mutex. The optional types are PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_ERRORCHECK, PTHREAD_MUTEX_RECURSIVE and PTHREAD.

2 pthread_mutex_lock pthread_mutex_unlock pthread_delay_np

The pthread_mutex_lock declaration begins to be locked with a mutex, and the code thereafter is locked until pthread_mutex_unlock is called, which means that only one thread can be called and executed at the same time. When a thread executes to pthread_mutex_lock, if the lock is being used by another thread at this time, the thread is blocked and the program waits until another thread releases the mutex.

Let's start with an example. We do this by creating two threads.


 #include

 #include

 #include

 #include

 #define MAX 10

 pthread_t thread[2];

 pthread_mutex_t mut;

 int number=0, i;

 void *thread1()

 {

 printf ("thread1 : I'm thread 1\n");

 for (i = 0; i < MAX; i++)

 {

 printf("thread1 : number = %d\n",number);

 pthread_mutex_lock(&mut);

 number++;

 pthread_mutex_unlock(&mut);

 sleep(2);

 }

 printf("thread1 : Is the main function waiting for me to finish the task? \n");

 pthread_exit(NULL);

 }

 void *thread2()

 {

 printf("thread2 : I'm thread 2\n");

 for (i = 0; i < MAX; i++)

 {

 printf("thread2 : number = %d\n",number);[nextpage]

 pthread_mutex_lock(&mut);

 number++;

 pthread_mutex_unlock(&mut);

 sleep(3);

 }

 printf("thread2 : Is the main function waiting for me to finish the task? \n");

 pthread_exit(NULL);

 }

 void thread_create(void)

 {

 int temp;

 memset(&thread, 0, sizeof(thread)); //comment1

 /* Create a thread */

 if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0) //comment2

 printf(" thread 1 Create a failure !\n");

 else

 printf(" thread 1 Be created \n");

 if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0) //comment3

 printf(" thread 2 Create a failure ");

 else

 printf(" thread 2 Be created \n");

 }

 void thread_wait(void)

 {

 /* Waiting for the thread to end */

 if(thread[0] !=0) { //comment4

 pthread_join(thread[0],NULL);

 printf(" thread 1 Has come to an end \n");

 }

 if(thread[1] !=0) { //comment5

 pthread_join(thread[1],NULL);

 printf(" thread 2 Has come to an end \n");

 }

 }

 int main()

 {

 /* Initialize the mutex with the default properties */

 pthread_mutex_init(&mut,NULL);

 printf(" I'm the main function oh, I'm creating a thread, hehe \n");

 thread_create();

 printf(" I am the main function. I am waiting for the thread to finish the task \n");

 thread_wait();

 return 0;

 }

Let's compile and execute 1 first

Quote:

falcon@falcon:~/program/c/code/ftp$ gcc -lpthread -o thread_example thread_example.c

falcon@falcon:~/program/c/code/ftp$ ./thread_example

I'm the main function oh, I'm creating a thread, hehe

Thread 1 is created

Thread 2 is created

I am the main function. I am waiting for the thread to finish the task

thread1 : I'm thread 1

thread1 : number = 0

thread2 : I'm thread 2

thread2 : number = 1

thread1 : number = 2

thread2 : number = 3

thread1 : number = 4

thread2 : number = 5

thread1 : number = 6

thread1 : number = 7

thread2 : number = 8

thread1 : number = 9

thread2 : number = 10

thread1: is the main function waiting for me to finish the task?

Thread 1 has ended

thread2: is the main function waiting for me to finish the task?

Thread 2 has ended


Related articles: