标签:
一、线程概念
如果进程需要完成多个任务的时候,需要对其进行串行化操作。而如果其中一个任务(比如io操作),造成任务执行的挂起。则可以分解任务,将任务分开执行。
其中的每个任务就是所谓的线程。
线程包含了表示进程内执行环境必需的信息。
进程的所有信息对该进程的所有线程都是共享的。包括可执行的程序文本、程序的全局内存和堆内存、栈以及文件描述符。
二、线程创建
新增的线程可以通过pthread_create()函数来创建。
pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void*), void*restrict arg );
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void thread(void)
{
int i;
sleep(1);
for(i=0;i<10;i++)
printf("This is a pthread.\n");
}
int main(void)
{
pthread_t id;
int i,ret;
ret=pthread_create(&id,NULL,(void *) thread,NULL);
// sleep(1);
if(ret!=0){
printf ("Create pthread error!\n");
exit (1);
}
for(i=0;i<3;i++)
printf("This is the main process.\n");
// pthread_join(id,NULL);
pthread_exit(NULL);
return (0);
}
当多个控制线程共享相同的内存的时候,需要确保每个线程看到一致的数据视图。
为了解决数据同步的问题,线程使用锁,也就是在同一时间只允许一个 线程访问该变量。
1、互斥量
我们可以通过使用pthread的互斥接口保护数据,确保同一时间只有一个线程访问数据。
互斥变量用pthread_mutex_t数据类型来表示。
对数据量加锁需要调用pthread_mutex_lock
对互斥量解锁,需要调用pthread_mutex_unlock
下面的代码没有经过加锁,应该是1和2连续输出的,但现在运行到一半时候可能被另一个线程打进来。
#include <stdio.h>
#include <pthread.h>
#include <malloc.h>
#include <string.h>
void* th_func(void* arg){
int i;
for(i=0; i<5; i++){
printf("1\n");
sleep(1);
printf("2\n");
}
}
int main(void){
int ret;
pthread_t tid1,tid2;
ret = pthread_create(&tid1, NULL, th_func, NULL);
if(ret != 0){
printf("pthread_create:%s\n",strerror(ret));
return -1;
}
ret = pthread_create(&tid2, NULL, th_func, NULL);
if(ret != 0){
printf("pthread_create:%s\n",strerror(ret));
return -1;
}
sleep(15);
return 0;
}
1 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 2
如果加上线程锁之后:
就避免了以上现象的发生。
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/a879365197/article/details/46710335