{
sem_t read
sem_t write
int reads
}
void RWLock_Init(rwlock &lock)
{
sem_init(lock.read, 0, 1)
sem_init(lock.write, 0, 1)
lock.reads = 0
}
void Acquir_read_lock(rwlock &lock)
{
sem_wait(&lock.read)
++lock.reads
if(lock.reads == 1)
{
sem_wait(&lock.write)
}
sem_post(&lock.write)
}
void acquir_read_unlock(rwlock &lock)
{
sem_wait(&lock.read)
--lock.reads
if(lock.reads == 0)
{
sem_post(&lock.write)
}
sem_post(&lcok.read)
}
void acquir_write_lock(rwlock &lock)
{
sem_wait(&lock.write)
}
void acquir_write_unlock(rwlock * lock)
{
sem_post(&lock.write)
}
线程参数,传地址使用动态分配吧,你只用一个buffer,后面的很可能就把前面的给覆盖了。把所有的输出printf使用一个互斥的信号量同步一下就行了吧,C的标准库不是线程安全的。
简单一点的,r,t分别产生两组随机数,你往后加参数即可:#include<stdlib.h>
#include<stdio.h>
int main(void){
int r=srandom((int)time(0))
int t=srandom((int)time(0))
printf("%d %d\n",r,t)
return 0
}
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)