#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
}
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)