About multithreading under Linux

  
        

First, thread creation

header file

#include <pthread.h>

function declaration

int pthread_create(pthread_t *thread , const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);

After the pthread_create function is called in a thread to create a new thread, the current thread continues to execute from pthread_create Start_routine is the entry function of the newly created thread. The start_routine function receives a parameter, which is passed to it by the arg parameter of pthread_create. The type of the parameter is void*. The type of the pointer is interpreted by the caller itself, and the return of start_routine is returned. The value type is also void. The meaning of this pointer is also defined by the caller. When start_routine returns, the thread exits. Other threads can call pthread_join to get the return value of start_routine.

Parameter Description

The first parameter is a pointer to a thread identifier.

The second parameter is used to set the thread properties to specify various thread properties.

The third parameter is the starting address of the thread running function. This function has only one universal pointer parameter arg. If you need to pass more than one parameter to the start_rtn function, you need to put these parameters into a structure. Then pass the address of this structure as an argument to arg.

The last parameter is the argument to the function.

Return Value

Returns 0 if the thread was created successfully. If the thread creation fails, the error number is returned and the contents of *thread are undefined.

The first simple thread program

#include <stdio.h>

#include <pthread.h>

void printThread( Const char *s)

{

pid_t pid;

pthread_t tid;

pid = getpid();

tid = Pthread_self();

printf("%s pid %u tid %u (0x%x)\ ", s, (unsigned int) pid, (unsigned int) tid, (unsigned int) tid );;

}

void* run(void* arg)

{

printThread("new thread: ");

return NULL;

}

int main(void)

{

pthread_t pt;

int err = pthread_create (&pt, NULL, run, NULL);

if (err != 0)

printf("can't create thread: %s\ ", strerror(err ));

printThread("main thread:");

pthread_join(pt, NULL);

return 0;

}

Note

Because the default pthread library is not a Linux system, but POSIX thread library. Use it as a library in Linux, so add -lpthread (or -pthread) to explicitly link the library.

$g++ main.cpp -lpthread -o main

$./main

Second, pthread_join function

Function declaration

Int pthread_join(pthread_t thread, void **retval);

Parameter Description

thread: The thread identifier, the thread ID, identifies the unique thread.

retval: A user-defined pointer that stores the return value of the thread being queued.

Description

The pthread_join function blocks the execution of the current thread to wait for the thread specified by thread to end. When the function returns, the resources of the waiting thread are reclaimed. If the process has ended, the function will return immediately. And the thread specified by thread must be joinable, the newly created thread is joinable by default.

Third, the two states of the thread: joinable (default) and detached

The joinable thread can be reclaimed by other threads, and the memory resources it occupies before being recycled by other threads are not Released, and the detached thread cannot be reclaimed by other threads, and the memory resources it occupies are automatically released by the system.

By default, the thread is joinable. Only when the pthread_join function returns, the created thread is terminated, and the system resources occupied by itself are released. The detached thread is not waiting by other threads, and the thread is executed. Immediately release system resources.

Set the thread state function pthread_attr_setdetachstate, the function is declared as pthread_attr_setdetachstate(pthread_attr_t* attr, int detachstate); the second parameter is optional PTHREAD_CREATE_DETACHED (separate thread) and PTHREAD _CREATE_JOINABLE (non-separating thread).

If you set up a thread as a separate thread, and this thread runs very fast, it is likely to terminate before the pthread_create function returns. After it terminates, it is possible to hand over the thread number and system resources to other threads. Use, so the thread that calls pthread_create gets the wrong thread number. One way to avoid this situation is to take some synchronization. One of the easiest ways is to call the pthread_cond_timewait function in the thread being created, and let the thread wait for a while, leaving enough time for the function pthread_create to return. Setting a wait time is a common method in multithreaded programming. But be careful not to use functions such as wait, which is to make the whole process sleep, and can not solve the problem of thread synchronization.

If a thread in the process executes pthread_detach(th), the th thread will be in the DETACHED state, which causes the th thread to free up its occupied memory resources at the end of the run, and it cannot be pthread_join( Synchronization, after pthread_detach() is executed, requesting pthread_join() on th will return an error.

Four, multi-threaded property settings under Linux

The default property value of attr

pthread_attr_t attr;

Initialization properties

pthread_attr_t Attr;

pthread_attr_init(&attr);

pthread_attr_init will return 0 upon successful completion. Any other return value indicates an error and will return the corresponding value.

Destroying attributes

Use pthread_attr_destroy(&attr) to delete the storage space allocated during initialization. The attribute object will be invalid. If the function completes successfully, it will return 0. Any other return value indicates The error is returned and the corresponding value will be returned.

Copyright © Windows knowledge All Rights Reserved