用C语言开多线程,想让多个相同的子线程同时运行,怎么实现

用C语言开多线程,想让多个相同的子线程同时运行,怎么实现,第1张

工作线程是处理后台工作的,创建一个线程非常简单,只需要两步:实线线程函数和开始线程不需要由CWinThread派生类,你可以不加修改地使用CWinThread。
AfxBeginThread有两种形式,一种是用来创建用户界面线程的,另一种就是用来创建工作线程的为了开始执行线程,只需要向AfxBeginThread提供下面的参数就可以了
1线程函数的地址
2传送到线程函数的参数
3(可选的)线程的优先级,可参阅::SetThreadPriority
4(可选的)线程开始时候的状态,可设置为CREATE_SUSPENEDE
5(可选的)线程的安全属性,请参阅SECURITY_ATTRIBUTES
实例代码
UINT ThreadProc(LPVOID pParam)
{
return 0;//线程成功完成
}
CWinThread AfxBeginThreadProc,//线程函数地址
LPVOID pParam,//线程参数
int nPriority=THREAD+PRIORITY_NOMAL,//线程优先级
int nStackSize=0,//线程堆栈大小,默认为1M
DWORD dwCreateFlags=0,
LPSECURITY_ATTRIBUTES lpSecurityAttrs=NULL
);

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

#include <windowsh>
#include <stdioh>
//#include <strsafeh>
DWORD WINAPI funcfirst( LPVOID lpParam ) 
{
 int i=0,j=0;
    while(1)
 {
  printf("hello,this thread 1 \n");
  
  //延时
  for(i=0;i<200000000;i++)
  {
   ;
  }
 }

DWORD WINAPI funcsecond( LPVOID lpParam ) 
{
 int i=0,j=0;
    while(1)
 {
  printf("hello,this thread 2 \n");
  
  //延时
  for(i=0;i<200000000;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<200000000;i++)
  {;}
  }
}

如果多行程序 写在一个批处理里面 那么只能一行一行运行
要想同时运行多个命令
那么可以在C编程的时候 引入多线程
创建多个线程,每个线程运行一行命令。
这样就可以实现多个命令同时执行了
多线程你自己百度一下,网上很多。执行命令就用system就可以了。

这是有可能当主线程(即main函数)执行完后,新创建的线程还没来得及执行,所以应该使主线程先暂停,即使用Sleep()函数
#include <windowsh>
#include <stdioh>
char testdir[100];
DWORD WINAPI Fun1( LPVOID lpParameter )
{
strcpy(testdir,"this is a test dir");
MessageBox(NULL,testdir,"线程",0);
return 0;
}
main()
{
HANDLE thread1;
thread1=CreateThread(NULL,0,Fun1,NULL,0,NULL);
CloseHandle(thread1);
printf("this is just a test:ok!\n");
Sleep(4000);
}

这是一个多线程例子,里面只有两个线程,是生产者/消费者模式,已编译通过,注释很详细,\x0d\如下:\x0d\\x0d\/ 以生产者和消费者模型问题来阐述Linux线程的控制和通信你 \x0d\ 生产者线程将生产的产品送入缓冲区,消费者线程则从中取出产品。\x0d\ 缓冲区有N个,是一个环形的缓冲池。\x0d\ /\x0d\#include \x0d\#include \x0d\\x0d\#define BUFFER_SIZE 16\x0d\\x0d\struct prodcons\x0d\{\x0d\ int buffer[BUFFER_SIZE];/实际存放数据的数组/\x0d\ pthread_mutex_t lock;/互斥体lock,用于对缓冲区的互斥 *** 作/\x0d\ int readpos,writepos; /读写指针/\x0d\ pthread_cond_t notempty;/缓冲区非空的条件变量/\x0d\ pthread_cond_t notfull;/缓冲区未满 的条件变量/\x0d\};\x0d\\x0d\/初始化缓冲区/\x0d\void pthread_init( struct prodcons p)\x0d\{\x0d\ pthread_mutex_init(&p->lock,NULL);\x0d\ pthread_cond_init(&p->notempty,NULL);\x0d\ pthread_cond_init(&p->notfull,NULL);\x0d\ p->readpos = 0;\x0d\ p->writepos = 0;\x0d\}\x0d\\x0d\/将产品放入缓冲区,这里是存入一个整数/\x0d\void put(struct prodcons p,int data)\x0d\{\x0d\ pthread_mutex_lock(&p->lock);\x0d\ /等待缓冲区未满/\x0d\ if((p->writepos +1)%BUFFER_SIZE ==p->readpos)\x0d\ {\x0d\ pthread_cond_wait(&p->notfull,&p->lock);\x0d\ }\x0d\ p->buffer[p->writepos] =data;\x0d\ p->writepos++;\x0d\ if(p->writepos >= BUFFER_SIZE)\x0d\ p->writepos = 0;\x0d\ pthread_cond_signal(&p->notempty);\x0d\ pthread_mutex_unlock(&p->lock);\x0d\}\x0d\/从缓冲区取出整数/\x0d\int get(struct prodcons p)\x0d\{\x0d\ int data;\x0d\ pthread_mutex_lock(&p->lock);\x0d\ /等待缓冲区非空/\x0d\ if(p->writepos == p->readpos)\x0d\ {\x0d\ pthread_cond_wait(&p->notempty ,&p->lock);//非空就设置条件变量notempty\x0d\ }\x0d\ /读书据,移动读指针/\x0d\ data = p->buffer[p->readpos];\x0d\ p->readpos++;\x0d\ if(p->readpos == BUFFER_SIZE)\x0d\ p->readpos = 0;\x0d\ /设置缓冲区未满的条件变量/\x0d\ pthread_cond_signal(&p->notfull);\x0d\ pthread_mutex_unlock(&p->lock);\x0d\ return data;\x0d\}\x0d\ /测试:生产站线程将1 到1000的整数送入缓冲区,消费者线程从缓冲区中获取整数,两者都打印信息/\x0d\#define OVER (-1)\x0d\struct prodcons buffer;\x0d\void producer(void data)\x0d\{\x0d\ int n;\x0d\ for( n=0;n\n",n);\x0d\ put(&buffer,n);\x0d\ }\x0d\ put(&buffer,OVER);\x0d\ return NULL;\x0d\}\x0d\void consumer(void data)\x0d\{\x0d\ int d;\x0d\ while(1)\x0d\ {\x0d\ d = get(&buffer);\x0d\ if(d == OVER)\x0d\ break;\x0d\ else\x0d\ printf("----->%d\n",d);\x0d\ }\x0d\ return NULL;\x0d\}\x0d\int main()\x0d\{\x0d\ pthread_t th_p,th_c;\x0d\ void retval;\x0d\ pthread_init(&buffer);\x0d\ pthread_create(&th_p,NULL,producer,0);\x0d\ pthread_create(&th_c,NULL,consumer,0);\x0d\ /等待两个线程结束/\x0d\ pthread_join(th_p, &retval);\x0d\ pthread_join(th_c,&retval);\x0d\ return 0;\x0d\}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12627495.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存