#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
void* print_message(void* arg) {
(void*)arg;
while(1)
{
printf("Hello from thread\n");
sleep(1);
}
return NULL;
}
int main() {
pthread_t threads;
pthread_create(&threads, NULL, print_message, NULL);
while(1)
{
sleep(1);
printf("main threads runing\n");
}
return 0;
}
创建一个线程:
extern int pthread_create (pthread_t *__restrict __newthread,
const pthread_attr_t *__restrict __attr,
void *(*__start_routine) (void *),
void *__restrict __arg) __THROWNL __nonnull ((1, 3));
- 创建成功返回0,否则返回错误编号。
- 新创建的线程ID设置成
__newthread指向的内存单元。 __attr用于定制各种不同的线程属性。__start_routine现成从这个函数地址开始运行。这个函数