All pastes #765954 Raw Edit

Something

public text v1 · immutable
·published 2007-11-08 14:21 UTC
rendered paste body
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>

int exitvariable = 1;
char sharedresource[1024] = "";
pthread_mutex_t resourcelocker = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;

void *readfromstdin(void *buf){
	while (1){
		pthread_mutex_lock(&resourcelocker);
		printf("Enter your string: ");
		fflush(stdout);
		scanf("%s",buf);
		strncpy(sharedresource,(char *)buf,sizeof(sharedresource));
		if(!strncmp((char *)buf,".",strlen((char*)buf))){
			exitvariable=0;
			pthread_exit(0);
		}
		pthread_mutex_unlock(&resourcelocker);
	}
}

void *writetostdout(void *parm){
	while(exitvariable){
		pthread_mutex_lock(&resourcelocker);
		printf("Entered text is: %s\n",sharedresource);
		pthread_mutex_unlock(&resourcelocker);
	}


}



int main(){
	char buf[1024];
	pthread_t reader;
	pthread_t writer;

	if(pthread_create(&reader,NULL,readfromstdin,buf))
		printf("Error creating first thread\n");

	if(pthread_create(&writer,NULL,writetostdout,NULL))
		printf("Error creating first thread\n");


	while(exitvariable){
		//sleep (1);
	}

	
	printf ("Exiting...\n");
}