linux线程同步--条件变量练习(1)

宁静致远 2012-08-06

注意:消息是由主线程产生的,而消息这时候在栈中,两个线程通过全局变量获取访问消息。

Unix环境高级编程P288

进程的所有信息对该进程的所有线程都是共享的,包括可执行的程序文本、程序的全局变量和堆内存、栈以及文件描述符。
#include <pthread.h>
#include <stdio.h>

struct msg {
	int data;
	struct msg *m_next;
	/* ... more stuff here ... */
};

struct msg *workq;
pthread_cond_t qready = PTHREAD_COND_INITIALIZER;
pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER;

	void
process_msg(void)
{
	struct msg *mp;

	for (;;) {
		pthread_mutex_lock(&qlock);
		while (workq == NULL)
			pthread_cond_wait(&qready, &qlock);
		mp = workq;
		workq = mp->m_next;
		pthread_mutex_unlock(&qlock);
		/* now process the message mp */
		printf("deal the mp. the data is %d\n",mp->data);
	}
}

	void
enqueue_msg(struct msg *mp)
{
	pthread_mutex_lock(&qlock);
	mp->m_next = workq;
	workq = mp;
	pthread_mutex_unlock(&qlock);
	pthread_cond_signal(&qready);
}
void* thr_fn(void* arg){
	process_msg();
	return ((void*)1);
}
int main(){
	pthread_t tid1;
	pthread_t tid2;
	int err;
	err = pthread_create(&tid1,NULL,thr_fn,NULL);
	if(err !=0){

		err_quit("can't  create thread%s\n",strerror(err));
	}
	
	err = pthread_create(&tid2,NULL,thr_fn,NULL);
	if(err !=0){
		err_quit("cant' create thread%s\n",strerror(err));
	}
	printf("create success.\n");
	sleep(1);
	struct msg msg1;
	msg1.data = 110;
	msg1.m_next = NULL;
	workq = NULL;
	enqueue_msg(&msg1);
	printf("add a msg\n");
	sleep(3);
	//pthread_cancel(tid1);
	//pthread_cancel(tid2);		
	printf("ok\n");
}

前面的程序访问的是主线程的栈区,下面的程序访问的是一个子线程的栈区。

可见线程之间是可以互相访问栈区的。

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>

struct msg {
	int data;
	struct msg *m_next;
	/* ... more stuff here ... */
};

struct msg *workq;
pthread_cond_t qready = PTHREAD_COND_INITIALIZER;
pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER;

	void
process_msg(void)
{
	struct msg *mp;

	for (;;) {
		pthread_mutex_lock(&qlock);
		while (workq == NULL)
			pthread_cond_wait(&qready, &qlock);
		mp = workq;
		workq = mp->m_next;
		pthread_mutex_unlock(&qlock);
		/* now process the message mp */
		printf("deal the mp. the data is %d\n",mp->data);
	}
}

void
enqueue_msg(struct msg *mp)
{
	pthread_mutex_lock(&qlock);
	mp->m_next = workq;
	workq = mp;
	pthread_mutex_unlock(&qlock);
	pthread_cond_signal(&qready);
}
void* thr_fn(void* arg){
	process_msg();
	return ((void*)1);
}
void* thr_fn2(void* arg){
	for(;;){
		struct msg temp;
		temp.data =120;
		temp.m_next = NULL;
		enqueue_msg(&temp);
		sleep(2);
	}
}
int main(){
	pthread_t tid1;
	pthread_t tid2;
	pthread_t tid3;
	int err;
	err = pthread_create(&tid1,NULL,thr_fn,NULL);
	if(err !=0){

		err_quit("can't  create thread%s\n",strerror(err));
	}
	
	err = pthread_create(&tid2,NULL,thr_fn,NULL);
	if(err !=0){
		err_quit("cant' create thread%s\n",strerror(err));
	}
	err = pthread_create(&tid3,NULL,thr_fn2,NULL);
	if(err!=0){
		err_quit("cant' create thread%s\n",strerror(err));
	}
	printf("create success.\n");
	//enqueue_msg(mp);
	//sleep(5);	
	sleep(20);
	pthread_cancel(tid1);
	pthread_cancel(tid2);
	//pthread_cancel(tid1);
	//pthread_cancel(tid2);		
	printf("ok\n");
}
 

相关推荐