a。A线程循环打印buf字符串,
即buf中本来存储1234567,倒置后buf中存储7654321.不打印!B线程循环倒置buf字符串,b
c。倒置不允许使用辅助数组。
d.要求A线程打印出来的结果只能为 1234567 或者 7654321
e。不允许使用sleep函数
具体代码如下:
#include<stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
char buf[]="1234567";
void* CallBack(void* arg)
{
while(1)
{
printf("%s\n",buf);
}
return NULL;
}
int main(int argc, const char *argv[])
{
pthread_t tid;
if(pthread_create(&tid,NULL,CallBack,NULL)!= 0)
{
fprintf(stderr,"pthread_create failed");
return -1;
}
while(1)
{
sleep(1);
char t;
int i=0;
int j=strlen(buf)-1;
while(i<j)
{
t=buf[i]; buf[i]=buf[j]; buf[j]=t;
i++;j--;
}
}
return 0;
}
运行结果如下: