From c8927987f85cb956a573630a6ee1103e82b0d5b1 Mon Sep 17 00:00:00 2001 From: weidongshan Date: Thu, 18 May 2023 16:25:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=80=9A=E7=94=A8Makefile?= =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=B9=B6=E8=A1=8Cmake=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Makefile_and_readme/Makefile.build | 4 +- .../source/13_thread/02_视频配套源码/pthread6_spinlock.c | 54 +++++++++++++ .../source/13_thread/02_视频配套源码/pthread7_rwlock.c | 76 +++++++++++++++++++ 3 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 04_嵌入式Linux应用开发基础知识/source/13_thread/02_视频配套源码/pthread6_spinlock.c create mode 100644 04_嵌入式Linux应用开发基础知识/source/13_thread/02_视频配套源码/pthread7_rwlock.c diff --git a/04_嵌入式Linux应用开发基础知识/source/05_general_Makefile/Makefile_and_readme/Makefile.build b/04_嵌入式Linux应用开发基础知识/source/05_general_Makefile/Makefile_and_readme/Makefile.build index d312b5f..ad8d28f 100644 --- a/04_嵌入式Linux应用开发基础知识/source/05_general_Makefile/Makefile_and_readme/Makefile.build +++ b/04_嵌入式Linux应用开发基础知识/source/05_general_Makefile/Makefile_and_readme/Makefile.build @@ -36,8 +36,8 @@ __build : $(subdir-y) built-in.o $(subdir-y): make -C $@ -f $(TOPDIR)/Makefile.build -built-in.o : $(cur_objs) $(subdir_objs) - $(LD) -r -o $@ $^ +built-in.o : $(subdir-y) $(cur_objs) + $(LD) -r -o $@ $(cur_objs) $(subdir_objs) dep_file = .$@.d diff --git a/04_嵌入式Linux应用开发基础知识/source/13_thread/02_视频配套源码/pthread6_spinlock.c b/04_嵌入式Linux应用开发基础知识/source/13_thread/02_视频配套源码/pthread6_spinlock.c new file mode 100644 index 0000000..6a176b2 --- /dev/null +++ b/04_嵌入式Linux应用开发基础知识/source/13_thread/02_视频配套源码/pthread6_spinlock.c @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include + +pthread_spinlock_t spinlock; +int shared_data = 0; + +static void *my_thread_func (void *data) +{ + int i = (int)data; + while (1) + { + pthread_spin_lock(&spinlock); + shared_data++; + printf("Thread %d is modifying the shared data: %d\n", i, shared_data); + pthread_spin_unlock(&spinlock); + sleep(1); + } + + return NULL; +} + + +int main(int argc, char **argv) +{ + pthread_t tid; + int ret; + int i; + + pthread_spin_init(&spinlock, PTHREAD_PROCESS_PRIVATE); + + /* 创建线程 */ + for (i =0; i < 10; i++) + { + ret = pthread_create(&tid, NULL, my_thread_func, (void *)i); + if (ret) + { + printf("pthread_create err!\n"); + return -1; + } + } + + + while (1) + { + sleep(100); + } + + pthread_spin_destroy(&spinlock); + return 0; +} + diff --git a/04_嵌入式Linux应用开发基础知识/source/13_thread/02_视频配套源码/pthread7_rwlock.c b/04_嵌入式Linux应用开发基础知识/source/13_thread/02_视频配套源码/pthread7_rwlock.c new file mode 100644 index 0000000..74d976d --- /dev/null +++ b/04_嵌入式Linux应用开发基础知识/source/13_thread/02_视频配套源码/pthread7_rwlock.c @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include + +pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER; +int shared_data = 0; + +static void *my_thread_write_func (void *data) +{ + int i = (int)data; + while (1) + { + pthread_rwlock_wrlock(&rwlock ); + shared_data++; + printf("Thread %d is modifying the shared data: %d\n", i, shared_data); + pthread_rwlock_unlock(&rwlock ); + sleep(1); + } + + return NULL; +} + +static void *my_thread_read_func (void *data) +{ + int i = (int)data; + while (1) + { + pthread_rwlock_rdlock(&rwlock ); + printf("Thread %d is reading the shared data: %d\n", i, shared_data); + pthread_rwlock_unlock(&rwlock ); + sleep(1); + } + + return NULL; +} + + +int main(int argc, char **argv) +{ + pthread_t tid; + int ret; + int i; + + /* 创建写线程 */ + for (i =0; i < 10; i++) + { + ret = pthread_create(&tid, NULL, my_thread_write_func, (void *)i); + if (ret) + { + printf("pthread_create err!\n"); + return -1; + } + } + + /* 创建读线程 */ + for (i =0; i < 10; i++) + { + ret = pthread_create(&tid, NULL, my_thread_read_func, (void *)i); + if (ret) + { + printf("pthread_create err!\n"); + return -1; + } + } + + while (1) + { + sleep(100); + } + + pthread_spin_destroy(&rwlock ); + return 0; +} +