linux c最简单多线程例子:

来源:互联网 发布:js封装 编辑:程序博客网 时间:2024/06/05 02:38

#include <stdio.h>
#include <pthread.h>
#define SIZEOFTHREAD 4
#define THREADNUM 3

pthread_t thread[THREADNUM];


void *thread1(int tn)
{      
 while(1)
 {
  printf ("thread : I'm thread %d/n", tn);       
  sleep(2);
 }
}


void thread_create(void)
{
 int i;
 for ( i = 0; i < THREADNUM; i ++)
 {
  memset( &thread[i], 0, SIZEOFTHREAD);
  printf("turn to i:%d/n", i);
  pthread_create( &thread[i], NULL, thread1, i );       
  sleep(3);
 }
}


void thread_wait(void)
{
 pthread_join(thread[0],NULL); //线程挂起
}

 

int main()
{
 thread_create();
 thread_wait();
   return 0;
}

原创粉丝点击