[Assorted-commits] SF.net SVN: assorted:[1158] sandbox/trunk/src/cc/st
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-02-03 20:18:59
|
Revision: 1158 http://assorted.svn.sourceforge.net/assorted/?rev=1158&view=rev Author: yangzhang Date: 2009-02-03 20:18:52 +0000 (Tue, 03 Feb 2009) Log Message: ----------- added longblock, threads Added Paths: ----------- sandbox/trunk/src/cc/st/longblock.cc sandbox/trunk/src/cc/st/longblock.mk sandbox/trunk/src/cc/st/threads.cc Added: sandbox/trunk/src/cc/st/longblock.cc =================================================================== --- sandbox/trunk/src/cc/st/longblock.cc (rev 0) +++ sandbox/trunk/src/cc/st/longblock.cc 2009-02-03 20:18:52 UTC (rev 1158) @@ -0,0 +1,50 @@ +// Demo that the st_read_fully function does in fact re-yield in the middle of +// its operation, which makes sense when reading large amounts of data. + +#include <st.h> +#include <iostream> +#include <commons/st/st.h> + +using namespace commons; +using namespace std; + +enum { port = 9876, size = 100000000 }; // 100mb +char *rbuf, *sbuf; + +void r() { + st_netfd_t l = st_tcp_listen(port); + st_netfd_t c = st_accept(l, 0, 0, -1); + st_read_fully(c, rbuf, size, -1); +} + +void s() { + st_netfd_t s = st_tcp_connect("localhost", port, -1); + st_write(s, sbuf, size, -1); +} + +void f() { + int i = 0; + while (i < size) { + while (rbuf[i] == 0) st_usleep(1); + for (; i < size && rbuf[i] == 1; i++); + cout << i << endl; + } +} + +int main() { + rbuf = new char[size]; + sbuf = new char[size]; + for (int i = 0; i < size; i++) { + rbuf[i] = 0; + sbuf[i] = 1; + } + st_init(); + st_thread_t t = st_spawn(f); + st_thread_t t0 = st_spawn(r); + st_usleep(10000); + st_thread_t t1 = st_spawn(s); + st_join(t1); + st_join(t0); + st_join(t); + return 0; +} Added: sandbox/trunk/src/cc/st/longblock.mk =================================================================== --- sandbox/trunk/src/cc/st/longblock.mk (rev 0) +++ sandbox/trunk/src/cc/st/longblock.mk 2009-02-03 20:18:52 UTC (rev 1158) @@ -0,0 +1,2 @@ +longblock: + g++ -o longblock longblock.cc -lst -lstx -lresolv Added: sandbox/trunk/src/cc/st/threads.cc =================================================================== --- sandbox/trunk/src/cc/st/threads.cc (rev 0) +++ sandbox/trunk/src/cc/st/threads.cc 2009-02-03 20:18:52 UTC (rev 1158) @@ -0,0 +1,40 @@ +// Demonstrates ST compatibility with OS threads. + +#include <iostream> +#include <pthread.h> +#include <st.h> +#include <unistd.h> + +using namespace std; + +void *f(void *p) { + cout << "hello" << endl; + st_sleep(1); + cout << "goodbye" << endl; + return 0; +} + +void *F(void *p) { + cout << "HELLO" << endl; + sleep(1); + cout << "GOODBYE" << endl; + return 0; +} + +int main() { + st_init(); + st_thread_t t1 = st_thread_create(f, 0, 1, 0); + st_thread_t t2 = st_thread_create(f, 0, 1, 0); + + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_t T; + pthread_create(&T, &attr, F, 0); + + st_thread_join(t1, 0); + st_thread_join(t2, 0); + + pthread_join(T, 0); + + return 0; +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |