Home > Backend Development > C#.Net Tutorial > C++ Multithreaded Programming On Linux

C++ Multithreaded Programming On Linux

黄舟
Release: 2017-02-06 13:58:38
Original
1106 people have browsed it

C++ Multithreaded Programming On Linux

POSIX multi-threading model pthread.h function:

pthread_attr_t attr; //线程属性结构体,创建线程时加入的参数
pthread_attr_init( &attr ); //初始化  
pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_JOINABLE ); 
//是设///置你想要指定线程属性参数,这个参数表明这个线程是可以join连接的,join功//能表示主程序可以等线程结束后再去做某事,实现了主程序和线程同步功能  
pthread_t tid1, tid2; //保存两个线程id
int ret = pthread_create( &tid1, &attr, say_hello1, ( void* )&index1 ); //创建线程1
ret = pthread_create( &tid2, &attr, say_hello2, ( void* )&index2 );  //创建线程2
pthread_join( tid1, NULL ); //连接两个线程  
pthread_join( tid2, NULL );
Copy after login

Function used for mutex lock:

pthread_mutex_t sum_mutex; //互斥锁
pthread_mutex_init( &sum_mutex, NULL ); //对锁进行初始化
pthread_mutex_lock( &sum_mutex ); //占用锁
//do something here..
pthread_mutex_unlock( &sum_mutex ); //释放锁
pthread_mutex_destroy( &sum_mutex ); //对锁进行注销
Copy after login


Functions used by semaphores:

pthread_cond_t tasks_cond; //条件信号变量
pthread_cond_init( &tasks_cond, NULL ); //对条件信号变量进行初始化
pthread_cond_signal( &tasks_cond ); //条件满足, 发送信号
pthread_cond_wait( &tasks_cond, &tasks_mutex ); //等待信号
pthread_cond_destroy( &tasks_cond ); //对条件信号变量进行注销
Copy after login

The above is the content of C++ multi-threaded programming On Linux. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template