#include
#include
#include
#include
#include
#include
#define THREAD_NUM 25
typedef struct
{undefined
FILE *_fp
int _nThreadId//第几个线程
sem_t *_semLock
}IDD_THREAD_PARAM
void *ThreadFunc(void *args)
{undefined
char sLine[100+1]
FILE *fpRead = ((IDD_THREAD_PARAM *)args)->_fp
sem_t *semLock = ((IDD_THREAD_PARAM *)args)->_semLock
int nId = ((IDD_THREAD_PARAM *)args)->_nThreadId
sem_wait(semLock)
while(!feof(fpRead))
{undefined
memset(sLine,0,sizeof(sLine))
fgets(sLine,100,fpRead)
fprintf(stderr,"Thread ID-%d:%s",nId,sLine)
}
sem_post(semLock)
}
int main()
{undefined
pthread_t *pThreads
sem_t semLock
pThreads = (pthread_t *)malloc(THREAD_NUM*sizeof(pthread_t))
sem_init(&semLock,0,1)
FILE *fp = fopen("test.txt","r")
//开始线程循环
IDD_THREAD_PARAM param
for(int i=0i
{undefined
memset(param,0,sizeof(IDD_THREAD_PARAM))
param._fp = fp
param._nThreadId = i
param._semLock = &semLock
pthread_create((pThreads+i),NULL,ThreadFunc,param)
}
for(int i=0i
pthread_join(*(pThreads+i),NULL)
free(pThreads)
pThreads = NULL
fclose(fp)
fp = NULL
return 0
}
生产者,消费者互斥同步参考如下代码:
#include <stdio.h>#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <error.h>
#include <semaphore.h>
#define PRODUCER_NUM 10
#define CONSUMER_MUM 8
#define BUFFER_SIZE 20
#define SLEEP_TIME 1
#define error_exit( _msg_ ) error(EXIT_FAILURE, errno, _msg_)
int print()
void *consumer_thread(void *args)
void *producer_thread(void *args)
sem_t can_produce
sem_t can_consume
pthread_mutex_t mutex
int produce_index = 0
int consume_index = 0
int producer_id = 0
int consumer_id = 0
int buffer[BUFFER_SIZE] = {0}
int main()
{
int i
pthread_t producer[PRODUCER_NUM]
pthread_t consumer[CONSUMER_MUM]
int sinit1 = sem_init(&can_produce, 0, BUFFER_SIZE)
int sinit2 = sem_init(&can_consume, 0, 0)
if(sinit1 || sinit2)
error_exit("sem_init")
if(pthread_mutex_init(&mutex, NULL))
error_exit("pthread_mutex_init")
for(i=0 i<PRODUCER_NUM i++)
if(pthread_create(&producer[i], NULL, producer_thread, NULL))
error_exit("pthread_create")
for(i=0 i<CONSUMER_MUM i++)
if(pthread_create(&consumer[i], NULL, consumer_thread, NULL))
error_exit("pthread_create")
for(i=0 i<PRODUCER_NUM i++)
pthread_join(producer[i], NULL)
for(i=0 i<CONSUMER_MUM i++)
pthread_join(consumer[i], NULL)
}
void *producer_thread(void *args)
{
int id = producer_id++
while(1){
sleep(SLEEP_TIME)
pthread_mutex_lock(&mutex)
sem_wait(&can_produce)
printf("Producer id %d in %d.\n", id, produce_index)
buffer[produce_index] = 1
produce_index = (produce_index + 1) % BUFFER_SIZE
print()
sem_post(&can_consume)
pthread_mutex_unlock(&mutex)
}
return NULL
}
void *consumer_thread(void *args)
{
int id = consumer_id++
while(1){
sleep(SLEEP_TIME)
pthread_mutex_lock(&mutex)
sem_wait(&can_consume)
printf("Consumer id %d in %d.\n", id, consume_index)
buffer[consume_index] = 0
consume_index = (consume_index + 1) % BUFFER_SIZE
print()
sem_post(&can_produce)
pthread_mutex_unlock(&mutex)
}
return NULL
}
int print()
{
int i
printf("Buffer:\n")
for(i = 0 i < BUFFER_SIZE i++)
printf("___")
printf("\n")
for(i = 0 i < BUFFER_SIZE i++)
printf("|%d|", buffer[i])
printf("\n")
for(i = 0 i < BUFFER_SIZE i++)
printf("___")
printf("\n")
return 0
}
线程之间的同步和互斥解决的问题是线程对共同资源进行访问。Posix有两种方式:信号量和互斥锁;信号量适用同时可用的资源为多个的情况;互斥锁适用于线程可用的资源只有一个的情况
1、互斥锁:互斥锁是用加锁的方式来控制对公共资源的原子操作(一旦开始进行就不会被打断的操作)
互斥锁只有上锁和解锁两种状态。互斥锁可以看作是特殊意义的全局变量,因为在同一时刻只有一个线程能够对互斥锁进行操作;只有上锁的进程才可以对公共资源进行访问,其他进程只能等到该进程解锁才可以对公共资源进行操作。
互斥锁操作函数:
pthread_mutex_init()//初始化
pthread_mutex_lock()//上锁参数:pthread_mutex_t *mutex
pthread_mutex_trylock()//判断上锁 参数:pthread_mutex_t *mutex
pthread_mutex_unlock()//解锁参数:pthread_mutex_t *mutex
pthread_mutex_release()//消除互斥锁 参数:pthread_mutex_t *mutex
互斥锁分为快速互斥锁、递归互斥锁、检错互斥锁;在 init 的时候确定
int pthread_mutex_t(pthread_mutex_t *mutex, const pthread_mutex_t mutexattr)
第一个参数:进行操作的锁
mutexattr:锁的类型,默认快速互斥锁(阻塞)123456789
2、信号量:信号量本质上是一个计数器,在操作系统做用于PV原子操作;
P操作使计数器-1;V操作使计数器+1.
在互斥操作中可以是使用一个信号量;在同步操作中需要使用多个信号量,并设置不同的初始值安排它们顺序执行
sem_init() // 初始化操作
sem_wait() // P操作,计数器减一;阻塞参数:sem_t *sem
sem_trywait() // P操作,计数器减一;非阻塞 参数:sem_t *sem
sem_post()// V操作,计数器加一 参数:sem_t *sem
sem_destroy() // 销毁信号量参数:sem_t *sem
sem_init(sem_t *sem, int pshared, int value)
pshared用于指定多少个进程共享;value初始值
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)