C language - Thread

C program threads simultaneously perform many tasks, compared with the process, small code size and execution speed

Each execution thread function needs to check the error function call error

1. How can the calling thread?

Thread function return type is void *

Pthread.h header files need support

Create a thread:

pthread_t xxx;

pthread_create ( & xxx, NULL, name of the function performed, NULL)

Recycling thread

void * xxxx;

pthread_join (thread name, & xxxx)

To prevent the threads call the same variables, the need to establish a mutex

Create a mutex:

pthread_mutex_t xxx=PTHREAD_MUTEX_INITIALIZER

Use mutex

pthread_mutex_lock(&xxx);

Open mutex

pthread_mutex_unlock(&xxx);

When used pthread.h headers, gcc compiler source code needs to be added to -lpthread connection parameters pthread library

Guess you like

Origin www.cnblogs.com/renren-study-notes/p/11784962.html