Browse thread
Asynchronous IO programming in OCaml
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Jérémie Dimino <jeremie@d...> |
| Subject: | Re: [Caml-list] Asynchronous IO programming in OCaml |
On Mon, Oct 25, 2010 at 11:34:41AM -0400, Yaron Minsky wrote:
> I don't quite understand how this whole benchmark holds together. Could
> you post the C code? I don't understand the differences between (1), (2)
> and (3) well enough to explain where the factor of 100 comes in.
Yes. Here is the code of the first program:
,----
| #include <sys/types.h>
| #include <sys/stat.h>
| #include <fcntl.h>
| #include <unistd.h>
|
| int main()
| {
| int fd = open("data", O_RDONLY);
| char buffer[4096];
|
| while (read(fd, buffer, 4096) > 0);
|
| close(fd);
|
| return 0;
| }
`----
the code of the second:
,----
| #include <sys/types.h>
| #include <sys/stat.h>
| #include <fcntl.h>
| #include <unistd.h>
| #include <pthread.h>
|
| int fd;
| char buffer[4096];
| int done = 0;
|
| void *callback(void* data)
| {
| int count = read(fd, buffer, 4096);
| if (count == 0) done = 1;
| return NULL;
| }
|
| int main()
| {
| fd = open("data", O_RDONLY);
|
| while (!done) {
| pthread_t thread;
| pthread_create(&thread, NULL, callback, NULL);
| pthread_join(thread, NULL);
| }
|
| close(fd);
|
| return 0;
| }
`----
and the third:
,----
| #include <sys/types.h>
| #include <sys/stat.h>
| #include <fcntl.h>
| #include <unistd.h>
| #include <pthread.h>
|
| int fd;
| char buffer[4096];
| int done = 0;
| pthread_cond_t start = PTHREAD_COND_INITIALIZER;
| pthread_cond_t stop = PTHREAD_COND_INITIALIZER;
| pthread_mutex_t start_mutex = PTHREAD_MUTEX_INITIALIZER;
| pthread_mutex_t stop_mutex = PTHREAD_MUTEX_INITIALIZER;
|
| void *callback(void* data)
| {
| while (!done) {
| pthread_cond_wait(&start, &start_mutex);
|
| int count = read(fd, buffer, 4096);
| if (count == 0) done = 1;
|
| pthread_mutex_lock(&stop_mutex);
| pthread_cond_signal(&stop);
| pthread_mutex_unlock(&stop_mutex);
| }
| return NULL;
| }
|
| int main()
| {
| fd = open("data", O_RDONLY);
|
| pthread_cond_init(&start, NULL);
| pthread_cond_init(&stop, NULL);
|
| pthread_mutex_lock(&start_mutex);
| pthread_mutex_lock(&stop_mutex);
|
| pthread_t thread;
| pthread_create(&thread, NULL, callback, NULL);
|
| while (!done) {
| pthread_mutex_lock(&start_mutex);
| pthread_cond_signal(&start);
| pthread_mutex_unlock(&start_mutex);
|
| pthread_cond_wait(&stop, &stop_mutex);
| }
|
| pthread_join(thread, NULL);
| close(fd);
|
| return 0;
| }
`----
Jérémie