gcc <文件名>.c -lpthread -o <文件名>
pthread不是Linux c 标准库。
本来是看着《现代操作系统》写的,但是《现代操作系统》的代码跑不动,找了一个博主能跑的,但他写的真是…
一个能跑的简单线程代码
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#define NUMBER_OF_THREAD 10
void thread(void)
{
printf("This is a pthread.\n");
}
int main(void)
{
pthread_t t[NUMBER_OF_THREAD];
int i, status;
for (i = 0; i < NUMBER_OF_THREAD; i++)
{
printf("This is the main process.\n");
status = pthread_create(&t[i], NULL, (void *)thread, NULL);
if (status != 0)
{
printf("error\n");
exit(1);
}
pthread_join(t[i], NULL);
}
return 0;
}