#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <xstring/xstring.h>
typedef struct {
int p_id[2];
short lock;
xstring *queue;
} _async_db;
void add_query(_async_db *query){
char msgbuf[1025];
char querybuffer[1025];
int nread;
while((nread=read(query->p_id[0],msgbuf,1024))>0){
query->lock=1;
msgbuf[nread]=0;
printf("sleeping 1\n");
sleep(1);
printf("sleeping 2\n");
sleep(1);
printf("sleeping 3\n");
sleep(1);
printf("sleeping 4\n");
sleep(1);
printf("sleeping 5\n");
sleep(1);
query->lock=0;
}
}
int main(){
_async_db *query;
query=malloc(sizeof(_async_db));
memset(query,0x0,sizeof(_async_db));
pipe(query->p_id);
pthread_t thread;
char testbuffer[20000];
int i;
for(i=0;i<1000;i++){
testbuffer[i]='A';
}
testbuffer[i]=0;
pthread_create(&thread,NULL,(void*)&add_query,query);
if(query->lock==0){
write(query->p_id[1],testbuffer,strlen(testbuffer));
}
if(query->lock==0){
write(query->p_id[1],"test\n",strlen("test\n"));
} else {
printf("query locked!\n");
}
if(query->lock==0){
write(query->p_id[1],"test\n",strlen("test\n"));
} else {
printf("query locked!\n");
}
sleep(10);
pthread_join(thread,NULL);
return(0);
} |