Get the App
SLTechnology News&Howtos  ›  Servers  › 

Application of unix multithread control

Shulou Source: shulou.com Published: 2022-06-02 14:38:21 10月18日 Update

1. Learning of three attributes of thread

Binding

Separate

Priority

Thread.c

# include "thread.h" # include#include#include#include#includeextern int status_1;extern int status_2;void thread_0 (char*pstr) / / with parameter {int counter = 10 position while (counter--) {sleep (1); printf ("% s\ n", pstr);} pthread_exit (& status_1); / / thread exit special function} void thread_1 (void) / / with no parameter {int counter = 10 position while (counter--) {sleep (1) Printf ("thread_1\ n");} pthread_exit (& status_2); / / thread exit special function} bool _ creat_thread (pthread_t * thread, void * (* start_routine) (void *), void * arg, int SCOPE, int DETACH, int policy, int priority_val) {struct sched_param param;pthread_attr_t attr If (0! = pthread_attr_init (& attr)) / / initialize the structure attr. If you do not need attr to cancel attr, pthread_attr_destroy {perror ("pthread_attr_init\ n"); return false;} if (0! = pthread_attr_setscope (& attr, SCOPE)) / / sets whether thread properties are bound (PTHREAD_SCOPE_SYSTEM) unbound (PTHREAD_SCOPE_PROCESS) {perror ("pthread_attr_setscope\ n"); return false } if (0! = pthread_attr_setdetachstate (& attr, DETACH)) / / sets whether the thread property is detached (PTHREAD_CREATE_DETACHED) non-detached (PTHREAD_CREATE_JOINABLE) {perror ("pthread_attr_setdetachstate\ n"); return false } if (0! = pthread_attr_setschedpolicy (& attr, policy)) / / three priority policy choices: SCHED_FIFO (values 1-99), SCHED_RR (values 1-99), and SCHED_OTHER {perror ("pead_attr_setschedpolicy\ n"); return false;} if (priority_val > 0 & priority_val)

Tags: Thread attribute priority parameter function policy dedicated three structure scope attachment learning scheduling selection multiline application control Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Technology Shulou Tech Info OPPO Reno Linux