diff --git a/04_day.pptx b/04_day.pptx index 2788fad..8c66017 100644 Binary files a/04_day.pptx and b/04_day.pptx differ diff --git a/04_day/thread/Android.bp b/04_day/thread/Android.bp index cebd80a..beefc4c 100644 --- a/04_day/thread/Android.bp +++ b/04_day/thread/Android.bp @@ -1,8 +1,8 @@ cc_binary { - name: "my_thread_1", + name: "my_ipc", shared_libs: ["liblog", "libutils", "libbinder"], srcs: [ - "Thread.cpp", + "IPCThreadState.cpp", ], } diff --git a/04_day/thread/IPCThreadState.cpp b/04_day/thread/IPCThreadState.cpp new file mode 100644 index 0000000..ddc9316 --- /dev/null +++ b/04_day/thread/IPCThreadState.cpp @@ -0,0 +1,40 @@ +#if 1 +#include +#include +#include +#include + +using namespace android; +//-------------------------------------------------------- + +class MyThread : public Thread +{ + public: + bool threadLoop() + { + IPCThreadState *ipc1 = IPCThreadState::self(); + IPCThreadState *ipc2 = IPCThreadState::self(); + printf("ipc1=%p\n", ipc1 ); + printf("ipc2=%p\n", ipc2 ); + return false; + } +}; +int main() +{ + sp thread1 = new MyThread; + sp thread2 = new MyThread; + thread1->run("MyThread1"); + thread2->run("MyThread2"); + thread1->join(); + thread2->join(); + + return 0; +} +#endif + + + + + + + diff --git a/04_day/thread/a.out b/04_day/thread/a.out index 22bb185..ad2461d 100644 Binary files a/04_day/thread/a.out and b/04_day/thread/a.out differ diff --git a/04_day/thread/tls.cpp b/04_day/thread/tls.cpp new file mode 100644 index 0000000..d6c94ac --- /dev/null +++ b/04_day/thread/tls.cpp @@ -0,0 +1,200 @@ +#if 1 +#include +#include +#include +#include + +pthread_key_t key; + +void foo(void) +{ + int *count = (int *)pthread_getspecific(key); + if( count == 0 ) + { + count = new int(0); + printf("count=%p\n", count); + pthread_setspecific(key, count); + } + printf("foo() : %d\n", ++*count ); +} +//------------------------------------------------------- +void *handler_1(void *data) +{ + foo(); + foo(); + foo(); + return 0; +} + +void *handler_2(void *data) +{ + foo(); + foo(); + return 0; +} +void my_destructor(void* p) +{ + printf("my_destructor(%p)\n", p ); + delete (int*)p; +} + +int main() +{ + pthread_t thread[2]; + pthread_key_create(&key, my_destructor); + printf("key=%d\n", key ); + pthread_create( &thread[0], 0, handler_1, 0 ); + pthread_create( &thread[1], 0, handler_2, 0 ); + pthread_join( thread[0], 0 ); + pthread_join( thread[1], 0 ); + return 0; +} +#endif + +#if 0 +#include +#include +#include +#include + +void foo(int *count) +{ + ++*count; + printf("foo() : %d\n", *count ); +} + +void *handler_1(void *data) +{ + int count=0; + foo(&count); + foo(&count); + foo(&count); + return 0; +} + +void *handler_2(void *data) +{ + int count=0; + foo(&count); + foo(&count); + return 0; +} + +int main() +{ + pthread_t thread[2]; + pthread_create( &thread[0], 0, handler_1, 0 ); + pthread_create( &thread[1], 0, handler_2, 0 ); + pthread_join( thread[0], 0 ); + pthread_join( thread[1], 0 ); + return 0; +} +#endif +#if 0 +#include +#include +#include +#include +void *foo(void *data) +{ + char ip[] = "192.168.56.100"; + char *p; + char *saveptr; + + p = strtok_r( ip , "." , &saveptr); + while(p) + { + printf("[%s]\n", p ); + p = strtok_r( saveptr , "." , &saveptr); + sleep(1); + } + return 0; +} +void *bar(void *data) +{ + char hp[] = "010-1234-5678"; + char *p; + char *saveptr; + + p = strtok_r( hp , "-" , &saveptr); + while(p) + { + printf("[%s]\n", p ); + p = strtok_r( saveptr , "-" , &saveptr); + sleep(1); + } + return 0; +} + +int main() +{ + pthread_t thread[2]; + pthread_create( &thread[0], 0, foo, 0 ); + pthread_create( &thread[1], 0, bar, 0 ); + pthread_join( thread[0], 0 ); + pthread_join( thread[1], 0 ); + return 0; +} +#endif +#if 0 +#include +#include +#include +#include +void *foo(void *data) +{ + char ip[] = "192.168.56.100"; + char *p; + + p = strtok( ip , "." ); + while(p) + { + printf("[%s]\n", p ); + p = strtok( 0 , "." ); + sleep(1); + } + return 0; +} +void *bar(void *data) +{ + char hp[] = "010-1234-5678"; + char *p; + + p = strtok( hp , "-" ); + while(p) + { + printf("[%s]\n", p ); + p = strtok( 0 , "-" ); + sleep(1); + } + return 0; +} + +int main() +{ + pthread_t thread[2]; + pthread_create( &thread[0], 0, foo, 0 ); + pthread_create( &thread[1], 0, bar, 0 ); + pthread_join( thread[0], 0 ); + pthread_join( thread[1], 0 ); + return 0; +} +#endif + +#if 0 +#include +#include +int main() +{ + char ip[] = "192.168.56.100"; + char *p; + + p = strtok( ip , "." ); + while(p) + { + printf("[%s]\n", p ); + p = strtok( 0 , "." ); + } + return 0; +} +#endif