linux线程与线程安全
2016-05-25 15:31
330 查看
linux线程与线程安全
0.在linux中创建一个线程
main.cpp
Makefile
引用了thead库
pthread_create()
创建一个线程默认的状态是joinable, 如果一个线程结束运行但没有被join,则它的状态类似于进程中的Zombie Process,即还有一部分资源没有被回收(退出状态码),所以创建线程者应该调用pthread_join来等待线程运行结束,并可得到线程的退出代码,回收其资源(类似于wait,waitpid)但是调用pthread_join(pthread_id)后,如果该线程没有运行结束,调用者会被阻塞,在有些情况下我们并不希望如此,比如在Web服务器中当主线程为每个新来的链接创建一个子线程进行处理的时候,主线程并不希望因为调用pthread_join而阻塞(因为还要继续处理之后到来的链接),这时可以在子线程中加入代码pthread_detach(pthread_self())或者父线程调用pthread_detach(thread_id)(非阻塞,可立即返回)
1.多线程互斥
使用:
例子:
2.多线程同步
使用pthread_mutex_t + pthread_cond_t + pthread_cond_wait + pthread_cond_signal
例子:
Makefile
其它见(重要:):
http://blog.csdn.net/monkey_d_meng/article/details/5628663
0.在linux中创建一个线程
main.cpp
#include <stdio.h> #include <pthread.h> void *thread_function(void *dummyPtr) { printf("Thread number %ld\n", pthread_self()); } int main(int argc, char *argv[]) { pthread_t thread_id; pthread_create( &thread_id, NULL, thread_function, NULL ); pthread_join( thread_id, NULL); printf("Final counter value: %d\n", counter); return 0; }
Makefile
引用了thead库
linuxthead:main.o g++ -g -o linuxthread main.o -L. -lpthread -lrt main.o:main.cpp g++ -g -c main.cpp -o main.o clean: rm -f *.o linuxthead
pthread_create()
创建一个线程默认的状态是joinable, 如果一个线程结束运行但没有被join,则它的状态类似于进程中的Zombie Process,即还有一部分资源没有被回收(退出状态码),所以创建线程者应该调用pthread_join来等待线程运行结束,并可得到线程的退出代码,回收其资源(类似于wait,waitpid)但是调用pthread_join(pthread_id)后,如果该线程没有运行结束,调用者会被阻塞,在有些情况下我们并不希望如此,比如在Web服务器中当主线程为每个新来的链接创建一个子线程进行处理的时候,主线程并不希望因为调用pthread_join而阻塞(因为还要继续处理之后到来的链接),这时可以在子线程中加入代码pthread_detach(pthread_self())或者父线程调用pthread_detach(thread_id)(非阻塞,可立即返回)
1.多线程互斥
pthread_mutex_t mutex; //互斥量声明 int g_counter = 0; //互斥量保护的全局变量 pthread_mutex_init(&mutex, NULL);
使用:
pthread_mutex_lock( &mutex1 ); g_counter++; pthread_mutex_unlock( &mutex1 )
例子:
#include <stdio.h> #include <pthread.h> pthread_mutex_t mutex; //互斥量声明 int counter = 0; //互斥量保护的全局变量 void *thread_function(void *dummyPtr) { printf("Thread number %ld\n", pthread_self()); pthread_mutex_lock( &mutex1 ); counter++; pthread_mutex_unlock( &mutex1 ); } int main(int argc, char *argv[]) { pthread_t thread_id[10]; int i, j; pthread_mutex_init(&mutex, NULL); for(i=0; i < 10; i++) { pthread_create( &thread_id[i], NULL, thread_function, NULL ); } for(j=0; j < 10; j++) { pthread_join( thread_id[j], NULL); } printf("Final counter value: %d\n", counter); return 0; }
2.多线程同步
使用pthread_mutex_t + pthread_cond_t + pthread_cond_wait + pthread_cond_signal
例子:
#include <iostream> #include <pthread.h> #include <string> #include <unistd.h> pthread_mutex_t count_lock; pthread_cond_t count_nonzero; unsigned count = 0; decrement_count () { pthread_mutex_lock (&count_lock); while(count==0) pthread_cond_wait( &count_nonzero, &count_lock); count=count -1; pthread_mutex_unlock (&count_lock); } increment_count() { pthread_mutex_lock(&count_lock); if(count==0) pthread_cond_signal(&count_nonzero); count=count+1; pthread_mutex_unlock(&count_lock); } int main(int argc, char* argv[]) { pthread_mutex_init(&count_lock, NULL); pthread_cond_init(&count_nonzero, NULL); pthread_t p1, p2; pthread_create(&p1, NULL, decrement_count, NULL); pthread_create(&p2, NULL, increment_count, NULL); pthread_join( p1, NULL); pthread_join( p2, NULL); }
Makefile
overpass:main.o g++ -g -o overpass main.o -L. -lhiredis -lpthread -lcurl -lrt -lcrypto -lidn -lssl main.o:main.cpp g++ -g -c main.cpp -o main.o clean: rm -f *.o overpass
其它见(重要:):
http://blog.csdn.net/monkey_d_meng/article/details/5628663
相关文章推荐
- Makefile常用函数
- LinuxC/C++编程(4)—管道通信
- Puppet 安装配置快速入门
- 整理一下常用的Linux命令(1)(未完待续)
- Linux命令- 文件与用户权限
- linux下解決ctrl+l 清屏失效
- Linux 通过sendmail 发邮件到外部邮箱
- MySql在Linux的启动方式
- Linux系统下openCV的配置安装及测试
- Linux 常用目录作用
- 基于linux 3.10.49内核 从dts文件里注册platform_device流程分析
- linux基础命令学习(十二)yum命令
- linux代理上网使用yum
- LINUX 论文
- Linux--进程组、会话、守护进程
- LoadRunner如何监控Linux下的系统资源
- linux下PostgreSQL安装和简单配置
- CentOS 6.5 生产环境编译安装LNMP
- 20135323符运锦----LINUX第三次实践:程序破解
- Linux入门:VI/VIM