apue- thread 线程学习(pthread_join,b
2019-12-11 本文已影响0人
国服最坑开发
0x01 等待子线程返回结果
pthread_join 会阻塞当前线程, 直到子线程返回结果
#include "apue.h"
#include <pthread.h>
void *sub(void *arg) {
printf("sub-thread-start ....\n");
sleep(2);
printf("sub-thread-end ....\n");
pthread_exit((void *) 180);
}
int main(int argc, char *argv[]) {
pthread_t p;
void *rst;
printf("main thread start ...\n");
int e = pthread_create(&p, NULL, sub, NULL);
if (e != 0)
err_exit(e, "can't create thread");
// main thread wait until get result from sub thread
e = pthread_join(p, &rst);
if (e != 0)
err_exit(e, "can't joint thread");
printf("main thread done %lu \n", (long)rst);
exit(0);
}
- 运行结果:
main thread start ...
sub-thread-start ....
sub-thread-end ....
main thread done 180
0x02 mutex 上锁超时
#include "apue.h"
#include <pthread.h>
int main(int argc, char *argv[]) {
int e;
struct timespec tout;
struct tm *tmp;
char buf[64];
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock(&lock);
printf("mutex is locked\n");
clock_gettime(CLOCK_REALTIME, &tout);
tmp = localtime(&tout.tv_sec);
strftime(buf, sizeof(buf), "%r", tmp);
printf("current time is %s\n", buf);
tout.tv_sec += 5;
// lock a mutex with timeout
e = pthread_mutex_timedlock(&lock, &tout);
clock_gettime(CLOCK_REALTIME, &tout);
tmp = localtime(&tout.tv_sec);
strftime(buf, sizeof(buf), "%r", tmp);
printf("the time is now %s \n", buf);
if (e == 0)
printf("mutex locked again!\n");
else
printf("can't lock mutex again: %s\n", strerror(e));
exit(0);
}
- 输出结果:
mutex is locked
current time is 10:38:44 AM
the time is now 10:38:49 AM
can't lock mutex again: Connection timed out
0x03 线程组同步 barrier
- 启用一个barrier并指定线程数 :
pthread_barrier_init(&b, NULL, TASK_CNT + 1);
- 每一个线程都会执行并等待到:
pthread_barrier_wait(&b);
当所有线程都到达这里后, 才都向后继续执行.
#include "apue.h"
#include <pthread.h>
#include <sys/time.h>
#define TASK_CNT ((unsigned int)5)
pthread_barrier_t b;
void *sub(void *arg) {
printf("[%lu]thread running within : %lu seconds\n", pthread_self(), (long) arg);
sleep((long) arg);
printf("[%lu]thread done\n", pthread_self());
pthread_barrier_wait(&b);
pthread_exit((void *) 0);
}
int main(int argc, char *argv[]) {
struct timeval start, end;
long long startusec, endusec;
double elapsed;
int err;
pthread_t tid;
gettimeofday(&start, NULL);
pthread_barrier_init(&b, NULL, TASK_CNT + 1);
srandom(1);
for (int i = 0; i < TASK_CNT; ++i) {
err = pthread_create(&tid, NULL, sub, (void *) (random() % 10));
if (err != 0)
err_exit(err, "can't create thread");
}
pthread_barrier_wait(&b);
gettimeofday(&end, NULL);
startusec = start.tv_sec * 1000000 + start.tv_usec;
endusec = end.tv_sec * 1000000 + end.tv_usec;
elapsed = (double) (endusec - startusec) / 1000000.0;
printf("all task took %.4f seconds\n", elapsed);
exit(0);
}
- 结果:
[140526034429696]thread running within : 3 seconds
[140526017644288]thread running within : 7 seconds
[140526026036992]thread running within : 6 seconds
[140526009251584]thread running within : 5 seconds
[140526000858880]thread running within : 3 seconds
[140526034429696]thread done
[140526000858880]thread done
[140526009251584]thread done
[140526026036992]thread done
[140526017644288]thread done
sort took 7.0024 seconds