当前位置: 技术问答>linux和unix
真的是不明白,高手帮指点下,程序怎么不按套路来了呢???
来源: 互联网 发布时间:2016-07-16
本文导语: #include #include #include #include #include #include void *thread_function(void *arg); pthread_mutex_t work_mutex; #define WORK_SIZE 1024 char work_area[WORK_SIZE]; int num1=0; int res; int main(){ pthread_t a_thread; void *thread_result; printf("why"); r...
#include
#include
#include
#include
#include
#include
void *thread_function(void *arg);
pthread_mutex_t work_mutex;
#define WORK_SIZE 1024
char work_area[WORK_SIZE];
int num1=0;
int res;
int main(){
pthread_t a_thread;
void *thread_result;
printf("why");
res = pthread_mutex_init(&work_mutex,NULL);
if(res != 0){
perror("Mutex initializatio failed");
exit(EXIT_FAILURE);
}
res = pthread_create(&a_thread,NULL,thread_function,NULL);//num1
if(res!=0){
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
pthread_mutex_lock(&work_mutex);//num2
printf("end");
}
void *thread_function(void *arg){
pthread_mutex_lock(&work_mutex);
num1++;
}
这是代码,编译运行结果是不输入任何东西,而是死循环的等待输入
其中的互斥量是在打印后面用的,怎么不打印“why”呢?
#include
#include
#include
#include
#include
void *thread_function(void *arg);
pthread_mutex_t work_mutex;
#define WORK_SIZE 1024
char work_area[WORK_SIZE];
int num1=0;
int res;
int main(){
pthread_t a_thread;
void *thread_result;
printf("why");
res = pthread_mutex_init(&work_mutex,NULL);
if(res != 0){
perror("Mutex initializatio failed");
exit(EXIT_FAILURE);
}
res = pthread_create(&a_thread,NULL,thread_function,NULL);//num1
if(res!=0){
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
pthread_mutex_lock(&work_mutex);//num2
printf("end");
}
void *thread_function(void *arg){
pthread_mutex_lock(&work_mutex);
num1++;
}
这是代码,编译运行结果是不输入任何东西,而是死循环的等待输入
其中的互斥量是在打印后面用的,怎么不打印“why”呢?
|
因为你程序退出前会刷新缓冲区,
改成
#include
int main(int argc, char *argv[])
{
printf("Hello, world");
while(1)
{
sleep(1);
}
return 0;
}
就不可以了
改成
#include
int main(int argc, char *argv[])
{
printf("Hello, world");
while(1)
{
sleep(1);
}
return 0;
}
就不可以了