《现代操作系统》 进程与线程
ubuntu16.04 codeblocks 环境下,需要codeblocks 在 poject>build options>linker settings 添加pthread,才能成功编译
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#define NUMBER_OF_THREADS 100
void *print(void *tid)
{
printf("hello world %d\n",tid);
pthread_exit(NULL);
}
int main()
{
pthread_t threads[NUMBER_OF_THREADS];
int status;
for(int i=0;i<NUMBER_OF_THREADS;i++)
{
printf("create thread %d\n",i);
status=pthread_create(&threads[i],NULL,print,(void *)i);
if(status!=0)
{
printf("THREAD ERROR %d",status);
exit(-1);
}
}
}