多线程编程
2017-03-23 15:40
113 查看
关于线程:
1、线程是调度的基本单位
2、进程是系统分配资源的基本单位
3、线程是在进程内部(地址空间)运行的
4、在Linux下,使用进程模拟线程,并没有真正意义上的线程,也称轻量级进程(LWP)
5、线程之间共享的部分:
地址空间、函数调用、全局变量、文件描述符表、每种信号的处理方式、当前工作目录、用户id,组id
6、线程私有:
线程ID、上下文各种寄存器的值(当再次切换调度时可恢复)、栈空间
7、线程的操作:
(1)线程的创建: 使用函数pthread_create(),原型为:int pthread_create(pthread_t restrict tidp,const pthread_attr_t *restrict_attr,void(start_rtn)(void),void *restrict arg);
若成功则返回0,否则返回出错误码;
第一个参数为指向线程标识符的指针。
第二个参数用来设置线程属性。
第三个参数是线程运行函数的起始地址。
第四个参数是运行函数的参数。
(2)线程等待:使用函数pthread_join();原型为:
int pthread_join(pthread_t thread, void **retval);
若成功则返回0,否则返回出错误码;
(3)线程终止:
a: 从线程函数return。这种⽅法对主线程不适用,从main函数return相当于调⽤exit。
b: ⼀个线程可以调⽤pthread_cancel终⽌止同⼀进程中的另⼀个线程。
c:线程可以调用pthread_exit终⽌止⾃己。
d:不能调用exit();exit是用来结束进程的。
调用exit:
从线程函数return:
调用pthread_exit():
(4)线程的分离:detached,创建出的线程默认可结合的,此时必须有特定的线程等待它回收资源,线程是可分离的,不需要等待,最后由系统回收资源;
(5)线程的互斥:
多个线程同时访问共享数据时可能会冲突,这跟前面讲信号时所说的可重⼊性是同样的问
题。⽐如 两个线程都要把某个全局变量增加1,这个操作在某平台需要三条指令完成:
1. 从内存读变量值到寄存器
2. 寄存器的值加1
3. 将寄存器的值写回内存
假设两个线程在调度切换时执⾏这三条指令,则可能导致最后变量只加了⼀次⽽非两次。我们创建两个线程,各⾃自把count增加5000次,正常情况下最后count应该等于10000,但事实上每次运行该程序的结果都不一样,有时候数到5000多,有时候数到6000多。
对于多线程的程序,访问冲突的问题是很普遍的,解决的办法是引入互斥锁(Mutex,MutualExclusive Lock),获得锁的线程可以完成“读-修改-写”的操作,然后释放锁给其它线程,没有获得锁的线程只能等待而不能访问共享数据,这样“读-修改-写”三步操作组成⼀个原⼦子操作,要么都执⾏,要么都不执行,不会执⾏到中间被打断,也不会在其它处理器上并行做这个操作。
1、线程是调度的基本单位
2、进程是系统分配资源的基本单位
3、线程是在进程内部(地址空间)运行的
4、在Linux下,使用进程模拟线程,并没有真正意义上的线程,也称轻量级进程(LWP)
5、线程之间共享的部分:
地址空间、函数调用、全局变量、文件描述符表、每种信号的处理方式、当前工作目录、用户id,组id
6、线程私有:
线程ID、上下文各种寄存器的值(当再次切换调度时可恢复)、栈空间
7、线程的操作:
(1)线程的创建: 使用函数pthread_create(),原型为:int pthread_create(pthread_t restrict tidp,const pthread_attr_t *restrict_attr,void(start_rtn)(void),void *restrict arg);
若成功则返回0,否则返回出错误码;
第一个参数为指向线程标识符的指针。
第二个参数用来设置线程属性。
第三个参数是线程运行函数的起始地址。
第四个参数是运行函数的参数。
(2)线程等待:使用函数pthread_join();原型为:
int pthread_join(pthread_t thread, void **retval);
若成功则返回0,否则返回出错误码;
#include<stdio.h> #include<pthread.h> #include<string.h> #include<stdlib.h> void* thread_run(void* arg) { int i = 5000; while(i--) { printf("newThreadtid:%lu,pid:%d\n",pthread_self(),get pid()); sleep(1); } return 0; } int main() { pthread_t id; int ret = pthread_create(&id,NULL,thread_run,NULL); if(ret!=0) { printf("%d,%s\n",ret,strerror); } int count = 0; while(count++<5) { printf("mainThreadtid:%lu,pid:%d\n",pthread_self(),getpid()); sleep(1); } return 0; }
(3)线程终止:
a: 从线程函数return。这种⽅法对主线程不适用,从main函数return相当于调⽤exit。
b: ⼀个线程可以调⽤pthread_cancel终⽌止同⼀进程中的另⼀个线程。
c:线程可以调用pthread_exit终⽌止⾃己。
d:不能调用exit();exit是用来结束进程的。
#include<stdio.h> #include<pthread.h> #include<string.h> #include<stdlib.h> void* thread_run(void* arg) { int i = 5000; while(i--) { printf("newThread tid :%lu,pid:%d\n",pthread_self(),getpid()); sleep(1); printf("thread %lu count is %d\n",pthread_self(),count); } pthread_cancel(pthread_self()); //pthread_exit((void*)234); //exit(1); //return (void*)33; } int main() { pthread_t id; int ret = pthread_create(&id,NULL,thread_run,NULL); if(ret!=0) { printf("%d,%s\n",ret,strerror); } int count = 0; while(count++<5) { printf("mainThread tid :%lu,pid:%d\n",pthread_self(),getpid()); sleep(1); } void* val; pthread_join(id,&val); printf("%d\n",val); printf("newthread was quit,mainthread is ready to quit\n"); return 0; }
调用exit:
从线程函数return:
调用pthread_exit():
(4)线程的分离:detached,创建出的线程默认可结合的,此时必须有特定的线程等待它回收资源,线程是可分离的,不需要等待,最后由系统回收资源;
(5)线程的互斥:
多个线程同时访问共享数据时可能会冲突,这跟前面讲信号时所说的可重⼊性是同样的问
题。⽐如 两个线程都要把某个全局变量增加1,这个操作在某平台需要三条指令完成:
1. 从内存读变量值到寄存器
2. 寄存器的值加1
3. 将寄存器的值写回内存
假设两个线程在调度切换时执⾏这三条指令,则可能导致最后变量只加了⼀次⽽非两次。我们创建两个线程,各⾃自把count增加5000次,正常情况下最后count应该等于10000,但事实上每次运行该程序的结果都不一样,有时候数到5000多,有时候数到6000多。
#include<stdio.h> #include<pthread.h> #include<string.h> #include<stdlib.h> int count = 0; void* thread_run(void* arg) { int i = 5000; int val = 0; while(i--) { int val = count; printf("thread %lu count is %d\n",pthread_self(),count); count = val+1; } return 0; } int main() { pthread_t id1; pthread_t id2; pthread_create(&id1,NULL,thread_run,NULL); pthread_create(&id2,NULL,thread_run,NULL); pthread_join(id1,NULL); pthread_join(id2,NULL); printf("count is %d\n",count); return 0; }
对于多线程的程序,访问冲突的问题是很普遍的,解决的办法是引入互斥锁(Mutex,MutualExclusive Lock),获得锁的线程可以完成“读-修改-写”的操作,然后释放锁给其它线程,没有获得锁的线程只能等待而不能访问共享数据,这样“读-修改-写”三步操作组成⼀个原⼦子操作,要么都执⾏,要么都不执行,不会执⾏到中间被打断,也不会在其它处理器上并行做这个操作。
//申请互斥锁 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; int count = 0; void* thread_run(void* arg) { int i = 5000; int val = 0; while(i--) { //加锁 pthread_mutex_lock(&mutex); int val = count; printf("thread %lu count is %d\n",pthread_self(),count); count = val+1; //解锁 pthread_mutex_unlock(&mutex); } return 0; } int main() { pthread_t id1; pthread_t id2; pthread_create(&id1,NULL,thread_run,NULL); pthread_create(&id2,NULL,thread_run,NULL); pthread_join(id1,NULL); pthread_join(id2,NULL); printf("count is %d\n",count); pthread_mutex_destroy(&mutex); return 0; }
相关文章推荐
- Qt中的多线程编程
- 多线程编程
- 你所不知道的五件事情(并发多线程编程)
- VC多线程编程(转)
- Delphi-多线程编程[1] 认识多线程
- 多线程编程
- 多线程编程(1)
- 多线程编程(19) - 不使用同步工具, 手动协调线程依次执行
- 细谈linux下的posix多线程编程
- 多线程编程
- 多线程编程
- Linux下的多线程编程
- 内容补充:浅述WinForm多线程编程与Control.Invoke的应用
- 线程同步(信号量,互斥,条件变量) 分类: linux应用程序 2008-04-08 09:43 2587人阅读 评论(1) 收藏 举报 进行多线程编程,最头疼的就是那些共享的数据。因为你无法知道哪个线程会在哪个时候对它进行操作,你也无法
- linux 多线程编程(二)
- Windows多线程编程
- linux下的多线程编程
- Silverlight 4之全方位剖析:多线程编程
- Linux下的多线程编程
- linux下多进程、多线程编程