星期一, 10月 15, 2007

Pthread

1.程式中載入
2.compile時,加入-pthread參數

範例=========================
#include <stdio.h>
#include <stdlib.h>  //因為在fc系統下,exit會有warnning,include此就不會有了
#include <pthread.h> //unix下的多執行緒

void *mythread(void *arg)
{
for (;;) {
printf("thread\n");
sleep(1);
}
return NULL;
}

int main(void)
{
pthread_t th;

if (pthread_create(&th,NULL,mythread,NULL)!=0)
exit(0);

for (;;) {
printf("main process\n");
sleep(3);
}
}
執行結果:
main process
thread
thread
thread
main process
thread
thread
thread
main process
thread
thread
thread

3 則留言:

匿名 提到...

請問 開頭的#include要inlcude甚麼

匿名 提到...

請問開頭要include甚麼 ??

fishjerky 提到...

嗯~ 修好了~