pthread condition variable tests
This commit is contained in:
parent
73064ffdf6
commit
41ba001828
@ -339,7 +339,7 @@ pthread_cond_destroy(pthread_cond_t *cond)
|
||||
*cond = NULL;
|
||||
free(cnd);
|
||||
|
||||
return EINVAL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include <pthread.h>
|
||||
|
||||
pthread_mutex_t mtx;
|
||||
pthread_cond_t cnd;
|
||||
|
||||
void *
|
||||
thread_simple(void *arg)
|
||||
@ -33,6 +34,22 @@ thread_lock(void *arg)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *
|
||||
thread_cond(void *arg)
|
||||
{
|
||||
int i;
|
||||
int status;
|
||||
|
||||
for (i = 0; i < 100; i++) {
|
||||
status = pthread_cond_wait(&cnd, NULL);
|
||||
assert(status == 0);
|
||||
status = pthread_cond_signal(&cnd);
|
||||
assert(status == 0);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, const char *argv[])
|
||||
{
|
||||
@ -43,23 +60,25 @@ main(int argc, const char *argv[])
|
||||
|
||||
printf("PThread Test\n");
|
||||
|
||||
// Simple thread test
|
||||
// Simple thread Test
|
||||
printf("simple test\n");
|
||||
status = pthread_create(&thr, NULL, thread_simple, NULL);
|
||||
assert(status == 0);
|
||||
status = pthread_join(thr, &result);
|
||||
assert(status == 0);
|
||||
assert(result == NULL);
|
||||
printf("\n");
|
||||
|
||||
// Return value test
|
||||
// Return value Test
|
||||
printf("return value test\n");
|
||||
status = pthread_create(&thr, NULL, thread_simple, (void *)1);
|
||||
assert(status == 0);
|
||||
status = pthread_join(thr, &result);
|
||||
assert(status == 0);
|
||||
assert(result == (void *)1);
|
||||
printf("\n");
|
||||
|
||||
// Mutex test
|
||||
// Mutex Test
|
||||
printf("simple mutex lock test\n");
|
||||
status = pthread_mutex_init(&mtx, NULL);
|
||||
assert(status == 0);
|
||||
@ -69,6 +88,7 @@ main(int argc, const char *argv[])
|
||||
assert(status == 0);
|
||||
status = pthread_mutex_destroy(&mtx);
|
||||
assert(status == 0);
|
||||
printf("\n");
|
||||
|
||||
// Mutex Contention Test
|
||||
printf("contended mutex lock test\n");
|
||||
@ -86,6 +106,32 @@ main(int argc, const char *argv[])
|
||||
assert(status == 0);
|
||||
status = pthread_mutex_destroy(&mtx);
|
||||
assert(status == 0);
|
||||
printf("\n");
|
||||
|
||||
// Condition Variable Test
|
||||
printf("simple condition variable test\n");
|
||||
status = pthread_cond_init(&cnd, NULL);
|
||||
assert(status == 0);
|
||||
status = pthread_cond_signal(&cnd);
|
||||
assert(status == 0);
|
||||
status = pthread_cond_wait(&cnd, NULL);
|
||||
assert(status == 0);
|
||||
status = pthread_cond_destroy(&cnd);
|
||||
assert(status == 0);
|
||||
printf("\n");
|
||||
|
||||
printf("threaded condition variable test\n");
|
||||
status = pthread_cond_init(&cnd, NULL);
|
||||
assert(status == 0);
|
||||
for (i = 0; i < 100; i++) {
|
||||
status = pthread_cond_signal(&cnd);
|
||||
assert(status == 0);
|
||||
status = pthread_cond_wait(&cnd, NULL);
|
||||
assert(status == 0);
|
||||
}
|
||||
status = pthread_cond_destroy(&cnd);
|
||||
assert(status == 0);
|
||||
printf("\n");
|
||||
|
||||
printf("Success!\n");
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user