QQ网名大全

C语言多线程_c语言多线程实验报告

win和linux下稍有不同,给你个windows下的例子:

#include <windows.h>
#include <stdio.h>
//#include <strsafe.h>

DWORD WINAPI funcfirst( LPVOID lpParam ) 
{

 int i=0,j=0;
    while(1)
 {
  printf("hello,this thread 1 ...\n");
  
  //延时
  for(i=0;i<@@0;i++)
  {
   ;
  }
 }


DWORD WINAPI funcsecond( LPVOID lpParam ) 
{

 int i=0,j=0;
    while(1)
 {
  printf("hello,this thread 2 ...\n");
  
  //延时
  for(i=0;i<@@0;i++)
  {
   ;
  }
 }


void main()
{
      int i=0;
    //创建线程1
       CreateThread( 
            NULL,              // default security attributes
            0,                 // use default stack size  
            funcfirst,        // thread function 
            NULL,             // argument to thread function 
            0,                 // use default creation flags 
            NULL);           // returns the thread identifier 

    //创建线程2
        CreateThread( 
            NULL,              // default security attributes
            0,                 // use default stack size  
            funcsecond,        // thread function 
            NULL,             // argument to thread function 
            0,                 // use default creation flags 
            NULL);           // returns the thread identifier

  //让主线程进入循环,主线程若退出,子线程1,2会被系统“杀死”
        while(1)
  {
  printf("hello,this thread 0 ...\n");
  
  //延时
  for(i=0;i<@@0;i++)
  {;}

  }
}
佚名
2024-07-02 19:03:31
最佳回答
类似问题(10)
  • 佚名
    2024-07-02 16:06:42

    C语言怎么写线程代码

    通常使用CreateThread函数来创建新的线程.(Unix下使用pthread_create函数)首先指出,线程与线程之间,是并列关系,不会存在"父子线程"...

  • 佚名
    2024-07-02 23:37:03

    C语言线程简单例题

    您好,这样的:通过pthread_join得到的终止状态是不同的,总结如下:1.如果thread线程通过return返回,rval_ptr所指向的单元里存放的是...

  • 佚名
    2024-07-02 09:33:32

    C语言中的线程?

    给你推荐一些比较好的教程吧,你应该用得着: 漫谈C++ Builder多线程编程技术: http:///html/xueyuan/chengxukaifa/...

  • 佚名
    2024-07-02 18:36:31

    C语言怎样实现多线程?

    首先你要有控制蛇移动方向的全局变量(定义在main以外因为线程函数也要调用它,每次键盘输入都会修改它的值), 比如 char direction 'a' ==...

  • 佚名
    2024-07-02 22:41:08

    C/C++线程问题

    建两个createthread 分别 运行 两个对象

  • 佚名
    2024-07-02 02:59:26

    C语言如何使用线程池中的某个线程

    问别人问题,还这么牛叉,真心佩服不要给线程派任务,让线程空闲的时候,自己去领任务

  • 佚名
    2024-07-02 02:36:43

    c语言创建线程的时候实质上做了什么?

    #include#include#include#include#include#define MAX 10pthread_t thread[2];pthrea...

  • 佚名
    2024-07-02 19:49:15

    C语言视频教程推荐

    还是要把一般呢书看完 边看边做,观看视频还是不够的 推荐一本 C primer Plus

  • 佚名
    2024-07-02 16:23:23

    如何用VisualStudio写C语言程序

    文件菜单的 新建 里头点进去会出现 新建项目 的框左边选择 Visual C++ 项目右边选择 win32 控制台应用程序并在下面起好名字,选好你的项目的文件夹...

  • 佚名
    2024-07-02 08:00:00

    c语言编程心形图案

    你把这个心用文本的形式给我,这样不好看。