android ndk sleep,android ndk 线程使用与sleep

#include

#include  // sleep 的头文件

#include

pthread_cond_t cond;

pthread_mutex_t mutex;

struct timeval now;

struct timespec outtime;

// 单位毫秒

void LTSleep(int nHm) {

gettimeofday(&now, NULL);

now.tv_usec += 1000*nHm;

if (now.tv_usec > 1000000) {

now.tv_sec += now.tv_usec / 1000000;

now.tv_usec %= 1000000;

}

outtime.tv_sec = now.tv_sec;

outtime.tv_nsec = now.tv_usec * 1000;

pthread_cond_timedwait(&cond, &mutex, &outtime);

}

void* thread_run(void* arg) {

fatherclass* thisa = (fatherclass*)arg;

while (thisa->threadFlag) {

....

//sleep(thisa->sleepTime);

LTSleep(30);

}

__android_log_print(ANDROID_LOG_WARN, "thread_run", "exit");

pthread_exit(0);

return 0;

}

使用:

void fatherclass::start(){

pthread_t pt;

pthread_create(&pt, NULL, &thread_run, (void *)this);

}

这里写了一个sleep函数。因为sleep参数是秒,我想用毫秒。

另外:pthread_cond_timedwait 我只当计时器用了。其中前两个参数有很大价值。大家可以选择性学习一下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值