首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

线程代码, 执行没有线程的效果, 为何?

2012-09-20 
线程代码, 执行没有线程的效果, 为何? 请指教。C/C++ code#include stdio.h#include pthread.h#include

线程代码, 执行没有线程的效果, 为何? 请指教。

C/C++ code
#include <stdio.h>#include <pthread.h>#include <unistd.h>#include <stdlib.h>void* thread(void*){    int i;    char *uargv;    for(i=0;i<30;i++)        printf("This is a pthread.\n");    return (void*)uargv;}int main(void){    pthread_t id;    int i,ret;    ret=pthread_create(&id,NULL,&thread,NULL);    /*if(ret!=0){        printf ("Create pthread error!\n");        exit (1);    }*/    for(i=0;i<30;i++)        printf("This is the main process.\n");    pthread_join(id,NULL);        return (0);}


执行的结果两种打印完全没有交错的




[解决办法]
CPU太快了, 一个循环一次就跑完了.

在循环里加1秒的sleep。

热点排行