:- use_module(library(system)).

/*
Without mutex
*/
program1(Goal):-	
	first_solution(_,
				 	[strategy_1(Goal), 
				 	strategy_2(Goal)], 
				 	[]).

/*
With mutex
*/	
program2(Goal):-
	mutex_create(myMutex),
	first_solution(_,
				 	[strategy_1(Goal, myMutex), 
				 	strategy_2(Goal, myMutex)], 
				 	[]),
	mutex_destroy(myMutex).

strategy_1(Goal):-
	read_text_file('text_file.txt', Goal),
	write('Thread 1 says: '), write(Goal), nl.
	
strategy_2(Goal):-
	read_text_file('text_file.txt', Goal),
	write('Thread 2 says: '), write(Goal), nl.	
				 	
strategy_1(Goal, Mutex):-
	with_mutex(Mutex, read_text_file('text_file.txt', Goal)),
	write('Thread 1 says: '), write(Goal), nl.
	
strategy_2(Goal, Mutex):-
	with_mutex(Mutex, read_text_file('text_file.txt', Goal)),
	write('Thread 2 says: '), write(Goal), nl.	

read_text_file(File, Goal):-
        see(File),        
        read(Goal),        
        seen.		
        
       		 	