Linuxでのpthreadマルチスレッドプログラミング
#include
int pthread_create(pthread_t *restrict tidp,
const pthread_attr_t *restrict attr,
void *(*start_rtn)(void),
void *restrict arg);
Returns: 0 if OK, error number on failure
。
。
。
。
#include
#include
#include
#include
#include
#define ERROR -1
#define OK 0
/*************************
* :thread_function
* : ,
* :
* :
* :
* **********************/
void *thread_function(void *arg)
{
int i = 0;
for ( i = 0; i < 20; i++)
{
printf("Thread says hi,%d!
",i);
}
}
int main(void)
{
pthread_t mythread;
if ( pthread_create(&mythread,NULL,thread_function,NULL) )
{
printf("Error creating thread
");
return ERROR;
}
/* */
if ( pthread_join(mythread,NULL))
{
printf("Error Joining thread");
return ERROR;
}
printf("main over!
");
return OK;
}
gcc thread.c-o theadコンパイル:エラーを報告します.以下のように、
/tmp/ccIlZgdh.o: In function `main':
thread.c:(.text+0x65): undefined reference to `pthread_create'
thread.c:(.text+0x91): undefined reference to `pthread_join'
collect2: ld 1
pthreadのデフォルトはLinuxライブラリにないため、変更する必要があります.
gcc thread.c -o -lpthread thread
すぐ