第一关
#include <stdio.h>
#include <pthread.h>
/************************
* 参数start_routine: 函数指针,用于指向线程函数
* 参数arg: 是线程函数的参数
* 返回值: 返回线程ID
*************************/
pthread_t createThread(void *(*start_routine) (void *), void *arg)
{
pthread_t thread;
/********** BEGIN **********/
int ret = pthread_create(&thread, NULL, start_routine, arg);
/********** END **********/
return thread;
}
第二关
#include <stdio.h>
#include <pthread.h>
/************************
* 参数thread: 需要等待结束的线程ID号
* 返回值: 等待成功返回0,失败返回-1
* 提示: 忽略线程返回值
*************************/
int waitThread(pthread_t thread)
{
int ret = -1;
/********** BEGIN **********/
if(pthread_join(thread, NULL) == 0)
{
return 0;
}
else
/********** END **********/
return ret;
}
第三关
#include <stdio.h>
#include <pthread.h>
/************************
* 参数thread: 需要等待结束的线程ID号
* 返回值: 等待成功返回0,失败返回-1
* 提示: 忽略线程返回值
*************************/
int cancelThread(pthread_t thread)
{
int ret = -1;
/********** BEGIN **********/
if(pthread_cancel(thread)==0)return 0;
else
/********** END **********/
return ret;
}