linux下的多线程通过pthread实现,下面给个简单的例子。
#include <stdio.h> #include <stdlib.h> #include <pthread.h> void* thr_fn() { printf("this is a thread, tid = %dn", pthread_self()); printf("thread returnn"); return (void*)0; } int main() { printf("this is the main thread, pid = %dn", getpid()); pthread_t tid; int ret; ret = pthread_create(&tid, NULL, thr_fn, NULL); if (ret != 0) { printf("create thread errorn"); exit(1); } pthread_join(tid, NULL); printf("main thread returnn"); return0; }
执行输出如下:

主要涉及两个函数:
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg); //线程创建函数
int pthread_join(pthread_t thread, void **retval); //等待线程结束函数
原文:http://www.cnblogs.com/gattaca/p/4729244.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!