本文共 8432 字,大约阅读时间需要 28 分钟。
1 openwrt开发环境配置
2 查看当前系统所使用的的线程库:
getconf GNU_LIBPTHREAD_VERSION3 多线程
pthread_cond_wait用于等待某个特定的条件为真,pthread_cond_signal用于通知阻塞的线程某个特定的条件为真了。在调用者两个函数之前需要声明一个pthread_cond_t类型的变量,用于这两个函数的参数pthread_join用于等待某个线程退出,成功返回0,否则返回Exxx(为正数)
./Documentation/scheduler/sched-deadline.txt: pthread_join(thread, NULL);
./tools/usb/ffs-test.c: int ret = pthread_join(t->id, NULL); ./tools/usb/testusb.c: if (pthread_join (entry->thread, &retval)) ./tools/usb/testusb.c: perror (“pthread_join”); ./tools/perf/bench/numa.c: ret = pthread_join(pthreads[t], NULL); ./tools/perf/bench/futex-wake.c: ret = pthread_join(worker[i], NULL); ./tools/perf/bench/futex-wake.c: err(EXIT_FAILURE, “pthread_join”); ./tools/perf/bench/futex-lock-pi.c: ret = pthread_join(worker[i].thread, NULL); ./tools/perf/bench/futex-lock-pi.c: err(EXIT_FAILURE, “pthread_join”); ./tools/perf/bench/futex-wake-parallel.c: if (pthread_join(td[i].worker, NULL)) ./tools/perf/bench/futex-wake-parallel.c: err(EXIT_FAILURE, “pthread_join”); ./tools/perf/bench/futex-wake-parallel.c: ret = pthread_join(blocked_worker[i], NULL); ./tools/perf/bench/futex-wake-parallel.c: err(EXIT_FAILURE, “pthread_join”); ./tools/perf/bench/sched-pipe.c: ret = pthread_join(td->pthread, NULL); ./tools/perf/bench/futex-hash.c: ret = pthread_join(worker[i].thread, NULL); ./tools/perf/bench/futex-hash.c: err(EXIT_FAILURE, “pthread_join”); ./tools/perf/bench/futex-requeue.c: ret = pthread_join(worker[i], NULL); ./tools/perf/bench/futex-requeue.c: err(EXIT_FAILURE, “pthread_join”); ./tools/perf/bench/sched-messaging.c: pthread_join(id, &thread_status); ./tools/perf/util/event.c: pthread_join(synthesize_threads[i], NULL); ./tools/perf/tests/mmap-thread-lookup.c: err = pthread_join(threads[i].pt, NULL); ./tools/perf/builtin-top.c: pthread_join(thread, NULL); ./tools/lib/lockdep/tests/ABBA_2threads.c: pthread_join(t, NULL); ./tools/virtio/ringtest/main.c: ret = pthread_join(guest, &tret); ./tools/virtio/ringtest/main.c: ret = pthread_join(host, &tret); ./tools/virtio/virtio-trace/trace-agent.c: ret = pthread_join(rw_thread_per_cpu[cpu], NULL); ./tools/virtio/virtio-trace/trace-agent.c: pr_err(“pthread_join() error:%d (cpu %d)\n”, ret, cpu); ./tools/testing/selftests/timers/threadtest.c: pthread_join(pth[i], &tret); ./tools/testing/selftests/futex/functional/futex_requeue_pi.c: pthread_join(waiter[i], ./tools/testing/selftests/futex/functional/futex_requeue_pi.c: pthread_join(blocker, NULL); ./tools/testing/selftests/futex/functional/futex_requeue_pi.c: pthread_join(waker, NULL); ./tools/testing/selftests/futex/functional/futex_requeue_pi_signal_restart.c: pthread_join(waiter, NULL); ./tools/testing/selftests/futex/functional/futex_requeue_pi_mismatched_ops.c: pthread_join(child, NULL); ./tools/testing/selftests/futex/functional/futex_wait_private_mapped_file.c: pthread_join(thr, NULL); ./tools/testing/selftests/rcutorture/formal/srcu-cbmc/tests/store_buffering/test.c: if (pthread_join(tu, NULL)) ./tools/testing/selftests/rcutorture/formal/srcu-cbmc/tests/store_buffering/test.c: if (pthread_join(tpr, NULL)) ./tools/testing/selftests/vm/userfaultfd.c: if (pthread_join(background_threads[cpu], NULL)) ./tools/testing/selftests/vm/userfaultfd.c: if (pthread_join(uffd_threads[cpu], &_userfaults[cpu])) ./tools/testing/selftests/vm/userfaultfd.c: if (pthread_join(uffd_threads[cpu], NULL)) ./tools/testing/selftests/vm/userfaultfd.c: if (pthread_join(locking_threads[cpu], NULL)) ./tools/testing/selftests/vm/userfaultfd.c: if (pthread_join(uffd_mon, (void **)&userfaults)) ./tools/testing/selftests/vm/userfaultfd.c: if (pthread_join(uffd_mon, (void **)&userfaults)) ./tools/testing/selftests/powerpc/tm/tm-unavailable.c: rc = pthread_join(t0, &ret_value); ./tools/testing/selftests/powerpc/tm/tm-unavailable.c: pr_err(rc, “pthread_join”); ./tools/testing/selftests/powerpc/tm/tm-trap.c: rc = pthread_join(t0_ping, NULL); ./tools/testing/selftests/powerpc/tm/tm-trap.c: pr_error(rc, “pthread_join()”); ./tools/testing/selftests/powerpc/tm/tm-trap.c: rc = pthread_join(t1_pong, NULL); ./tools/testing/selftests/powerpc/tm/tm-trap.c: pr_error(rc, “pthread_join()”); ./tools/testing/selftests/powerpc/tm/tm-vmx-unavail.c: pthread_join(thread[i], NULL); ./tools/testing/selftests/powerpc/tm/tm-tmspr.c: if (pthread_join(thread, NULL) != 0) ./tools/testing/selftests/powerpc/tm/tm-tmspr.c: if (pthread_join(thread, NULL) != 0) ./tools/testing/selftests/powerpc/dscr/dscr_default_test.c: if (pthread_join(threads[i], (void **)&(status[i]))) { ./tools/testing/selftests/powerpc/dscr/dscr_default_test.c: perror(“pthread_join() failed”); ./tools/testing/selftests/powerpc/math/fpu_preempt.c: pthread_join(tids[i], &rc_p); ./tools/testing/selftests/powerpc/math/fpu_signal.c: pthread_join(tids[i], &rc_p); ./tools/testing/selftests/powerpc/math/vmx_signal.c: pthread_join(tids[i], &rc_p); ./tools/testing/selftests/powerpc/math/vsx_preempt.c: pthread_join(tids[i], &rc_p); ./tools/testing/selftests/powerpc/math/vmx_preempt.c: pthread_join(tids[i], &rc_p); ./tools/testing/selftests/powerpc/benchmarks/fork.c: rc = pthread_join(tid, NULL); ./tools/testing/selftests/powerpc/benchmarks/fork.c: perror(“pthread_join”); ./tools/testing/selftests/mqueue/mq_perf_tests.c: pthread_join(cpu_threads[i], NULL); ./tools/testing/selftests/mqueue/mq_perf_tests.c: pthread_join(cpu_threads[0], &retval); ./tools/testing/selftests/sync/sync_stress_consumer.c: pthread_join(threads[i], NULL); ./tools/testing/selftests/sync/sync_stress_parallelism.c: pthread_join(a, NULL); ./tools/testing/selftests/sync/sync_stress_parallelism.c: pthread_join(b, NULL); ./tools/testing/selftests/seccomp/seccomp_bpf.c: ASSERT_EQ(0, pthread_join(thread, &status)); ./tools/testing/selftests/seccomp/seccomp_bpf.c: ASSERT_EQ(0, pthread_join(thread, &status)); ./tools/testing/selftests/seccomp/seccomp_bpf.c: int _rc = pthread_join(tid, status); \ ./tools/testing/selftests/seccomp/seccomp_bpf.c: TH_LOG(“pthread_join of tid %u failed: %d\n”, \ ./tools/testing/selftests/seccomp/seccomp_bpf.c: /* Poll for actual task death. pthread_join doesn’t guarantee it. */ ./tools/testing/selftests/seccomp/seccomp_bpf.c: /* Poll for actual task death. pthread_join doesn’t guarantee it. */ ./tools/testing/selftests/x86/ldt_gdt.c: if (pthread_join(thread, NULL) != 0) ./tools/testing/selftests/x86/ldt_gdt.c: err(1, “pthread_join”); ./tools/testing/selftests/x86/fsgsbase.c: if (pthread_join(thread, NULL) != 0) ./tools/testing/selftests/x86/fsgsbase.c: err(1, “pthread_join”); ./tools/testing/selftests/bpf/test_lpm_map.c: assert(pthread_join(thread_id[i], &ret) == 0 && ret == (void *)&info[i]); ./tools/testing/radix-tree/iteration_check.c: if (pthread_join(threads[i], NULL)) { ./tools/testing/radix-tree/iteration_check.c: perror(“pthread_join”); ./tools/testing/radix-tree/multiorder.c: pthread_join(worker_thread[i], NULL); ./tools/testing/radix-tree/idr-test.c: pthread_join(threads[i], NULL); ./tools/testing/radix-tree/regression1.c: if (pthread_join(threads[i], NULL)) { ./tools/testing/radix-tree/regression1.c: perror(“pthread_join”); guang@guang-net:~/develop/linux$// mutex.c#include#include #include #include #define NUM_THREAD 100void *thread_inc (void *arg);void *thread_des (void *arg);long long num = 0;pthread_mutex_t mutex;int main (int argc, char *argv){ pthread_t thread_id[NUM_THREAD]; int i; pthread_mutex_init (&mutex, NULL); for (i = 0; i < NUM_THREAD; i++) { if (i%2) { pthread_create (&(thread_id[i]), NULL, thread_inc, NULL); } else { pthread_create (&(thread_id[i]), NULL, thread_des, NULL); } } for (i = 0; i < NUM_THREAD; i++) { pthread_join (thread_id[i], NULL); } printf ("result: %lld \n", num); pthread_mutex_destroy (&mutex); return 0;}void *thread_inc (void *arg){ int i; pthread_mutex_lock (&mutex); for (i = 0; i < 500; i++) { num += 1; } pthread_mutex_unlock (&mutex); return NULL;}void *thread_des (void *arg){ int i; for (i = 0; i < 500; i++) { pthread_mutex_lock (&mutex); num -= 1; pthread_mutex_unlock (&mutex); } return NULL;}
#includeint main (void){ int i; for (i = 0; i < 100; i ++) { printf ("%2d:%d ",i, i%2); if (!(i % 10)) { printf ("\n"); } } printf ("\n"); return 0;}
转载地址:http://bbvsi.baihongyu.com/