Summary about mutlti-threading in C and Java
1. If the main thread terminates, then the whole program dies, too. So if you
want to run some tasks in a sub thread, you must guarantee that sub thread
termination, you should use pthread_join rather than condition and mutex.
pthread_join is like waitpid, it suspend the calling thread until another
thread terminates and you can retrieve thread's termination status through
macros. So when you use them incorrectly, you will get very confucious
compiling error messages. For example,
...
static pthread_mutex_t lock;
...
43 lock = PTHREAD_MUTEX_INITIALIZER;
...
If you compile, you will get:
43: error: expected expression before token '{'
The most weird thing is that there is no such token '{'. We can imagine that
PTHREAD_MUTEX_INITIALIZER is implemented as macro.
In addition we have two ways to initialize mutex and conditions,
1. initialize them when declaring them.
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
2. use initializing functions after declaration.
pthread_mutex_init( &lock, NULL ); pthread_cond_init( &cond, NULL );
4. When you wait a condition, you must lock its mutex, when you signal a
condition, you MUST NOT lock the mutex. MAKE sure you change the condition
variable(auto variables, declared in the thread) becomes invalid when thread
exits. So you should NEVER return a auto variable or put an auto variable
into global data strutures.
1. If the main thread terminates, then the whole program dies, too. So if you
want to run some tasks in a sub thread, you must guarantee that sub thread
terminates before main thread.
termination, you should use pthread_join rather than condition and mutex.
pthread_join is like waitpid, it suspend the calling thread until another
thread terminates and you can retrieve thread's termination status through
pthread_join which is much more difficult to do using condition and mutex.
macros. So when you use them incorrectly, you will get very confucious
compiling error messages. For example,
...
static pthread_mutex_t lock;
...
43 lock = PTHREAD_MUTEX_INITIALIZER;
...
If you compile, you will get:
43: error: expected expression before token '{'
The most weird thing is that there is no such token '{'. We can imagine that
PTHREAD_MUTEX_INITIALIZER is implemented as macro.
In addition we have two ways to initialize mutex and conditions,
1. initialize them when declaring them.
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
2. use initializing functions after declaration.
pthread_mutex_init( &lock, NULL ); pthread_cond_init( &cond, NULL );
4. When you wait a condition, you must lock its mutex, when you signal a
condition, you MUST NOT lock the mutex. MAKE sure you change the condition
before signaling.
variable(auto variables, declared in the thread) becomes invalid when thread
exits. So you should NEVER return a auto variable or put an auto variable
into global data strutures.
本文深入探讨了C和Java中多线程的概念、关键原则及实践应用,包括线程生命周期、线程同步、互斥锁和条件变量的使用、线程局部存储的重要性,以及如何避免常见的多线程编程陷阱。
188

被折叠的 条评论
为什么被折叠?



