这么高的悬赏,实例放后面。信号量(sem),如同进程一样,线程也可以通过信号量来实现通信,虽然是轻量级的。信号量函数的名字都以"sem_"打头。线程使用的基本信号量函数有四个。
信号量初始化。int sem_init (sem_t *sem , int pshared, unsigned int value)
这是对由sem指定的信号量进行初始化,设置好它的共享选项(linux 只支持为0,即表示它是当前进程的局部信号量),然后给它一个初始值VALUE。
等待信号量。给信号量减1,然后等待直到信号量的值大于0。
int sem_wait(sem_t *sem)
释放信号量。信号量值加1。并通知其他等待线程。
int sem_post(sem_t *sem)
销毁信号量。我们用完信号量后都它进行清理。归还占有的一切资源。
int sem_destroy(sem_t *sem) #include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <errno.h>
#define return_if_fail(p) if((p) == 0){printf ("[%s]:func error!/n", __func__)return}
typedef struct _PrivInfo
{
sem_t s1
sem_t s2
time_t end_time
}PrivInfo
static void info_init (PrivInfo* thiz)
static void info_destroy (PrivInfo* thiz)
static void* pthread_func_1 (PrivInfo* thiz)
static void* pthread_func_2 (PrivInfo* thiz)
int main (int argc, char** argv)
{
pthread_t pt_1 = 0
pthread_t pt_2 = 0
int ret = 0
PrivInfo* thiz = NULL
thiz = (PrivInfo* )malloc (sizeof (PrivInfo))
if (thiz == NULL)
{
printf ("[%s]: Failed to malloc priv./n")
return -1
}
info_init (thiz)
ret = pthread_create (&pt_1, NULL, (void*)pthread_func_1, thiz)
if (ret != 0)
{
perror ("pthread_1_create:")
}
ret = pthread_create (&pt_2, NULL, (void*)pthread_func_2, thiz)
if (ret != 0)
{
perror ("pthread_2_create:")
}
pthread_join (pt_1, NULL)
pthread_join (pt_2, NULL)
info_destroy (thiz)
return 0
}
static void info_init (PrivInfo* thiz)
{
return_if_fail (thiz != NULL)
thiz->end_time = time(NULL) + 10
sem_init (&thiz->s1, 0, 1)
sem_init (&thiz->s2, 0, 0)
return
}
static void info_destroy (PrivInfo* thiz)
{
return_if_fail (thiz != NULL)
sem_destroy (&thiz->s1)
sem_destroy (&thiz->s2)
free (thiz)
thiz = NULL
return
}
static void* pthread_func_1 (PrivInfo* thiz)
{
return_if_fail(thiz != NULL)
while (time(NULL) < thiz->end_time)
{
sem_wait (&thiz->s2)
printf ("pthread1: pthread1 get the lock./n")
sem_post (&thiz->s1)
printf ("pthread1: pthread1 unlock/n")
sleep (1)
}
return
}
static void* pthread_func_2 (PrivInfo* thiz)
{
return_if_fail (thiz != NULL)
while (time (NULL) < thiz->end_time)
{
sem_wait (&thiz->s1)
printf ("pthread2: pthread2 get the unlock./n")
sem_post (&thiz->s2)
printf ("pthread2: pthread2 unlock./n")
sleep (1)
}
return
}
实现一个队列CQueue CQueue提供两个公有成员函数 addTail():往队列尾部增加一个元素 removeHead():读出并移除队列的第一个元素 生产者:两个线程通过调用CQueue::addTail()往队列中增加元素 消费者:一个线程通过调用CQueue::removeHead()从队列中读取元素 #include <iostream> #include <list> #include <windows.h> #include <process.h> using namespace std #define P(sem) WaitForSingleObject(sem,INFINITE) #define V(sem) ReleaseSemaphore(sem,1,NULL) class CQueue { public: void addTail()//往队列尾部增加一个元素 void removeHead()//读出并移除队列的第一个元素 private: list<int>L } CQueue buffer//全局的缓冲区 const int buf_size = 10//缓冲区大小 static int GOODS_ID = 0//商品序号 const int producers = 3//生产者数量 const int consumers = 8//消费者数量 void ProducerThread(void* param) void ConsumerThread(void* param) HANDLE empty,occupy,op_mutex int main() { int i int p_id[producers],c_id[consumers]#include <stdio.h>#include <time.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys.sem.h>
#define NEED_P 2
#define NEED_C 2
#define WORKS_P 10
#define WORKS_C 10
#define BUF_LENGTH(sizeof(struct(mybuffer))
#define BUF_NUM 10
#define SHM_MODE 0600
#define SEM_ALL_KEY 2411
#define SEM_EMPTY 0
#define SEM_FULL 1
struct mybuffer
{
char letter[BUF_NUM]
int head
int tail
int is_empty
}
int get_random()
{
int t
srand((unsigned)(getpid() + time(NULL)))
t=rand()%5
return t
}
char get_char()
{
char a
srand((unsigned)(getpid() + time(NULL)))
a =(char)((char)(rand()%26)+'A')
}
void p(int sem_id,int sem_num)
{
struct sembuff pc
pc.sem_num=sem_num
pc.sem_op=-1
pc.sem_flg=0
semop(sem_id,&pc,1)
}
void v(int sem_id,int sem_num)
{
struct sembuf pc
pc.sem_num=sem_num
pc.sem_op=1
pc.sem_flg=0
semop(sem_id,&pc,1)
}
int main(int argc,char* argv[])
{
int i,j
int shm_id,sem_id
int num_p=0,num_c=0
struct mybuffer* shmptr
char lt
time_t now
pid_t pid_p,pid_c
sem_id=semget(SEM_ALL_KEY,2,IPC_CREATE|0600)
IF(sem_id>=0)
printf(
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)