`
renzhelife
  • 浏览: 669840 次
文章分类
社区版块
存档分类
最新评论

Multithreading in C and Java

 
阅读更多
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

terminates before main thread.


2. If you want to execute some task in a separate thread and wait until its
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.


3. PTHREAD_MUTEX_INITIALIZER and PTHREAD_COND_INITIALIZER are implemented as
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.

2. use initializing functions after declaration.

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.


5. Thread has its local stack frame, just like a function. Any local
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.

分享到:
评论

相关推荐

    Java: High-Performance Apps with Java 9

    He was featured on the TV and in print media, including Fox News, NBC News, CBS News, Fox Business, Bloomberg International TV, Forbes, Inc. magazine, Daily Mirror, and The Huffington Post. He is also...

    JAVA白皮书(英文版)

    2.2Features Removed from C and C++ 2.2.1No More Typedefs, Defines, or Preprocessor 2.2.2No More Structures or Unions 2.2.3No Enums 2.2.4No More Functions 2.2.5No More Multiple Inheritance ...

    21天学通java (英文版)

    6 Creating Classes and Applications in Java 95 7 More About Methods 111 Week 2 at a Glance Day 8 Java Applet Basics 129 9 Graphics, Fonts, and Color 149 10 Simple Animation and Threads 173 11 More ...

    Learn Java for Android Development 3rd Edition mobi格式

    Learn Java for Android Development, Third Edition, is an update of a strong selling book that now includes a primer on Android app development (in Chapter 1 and Appendix C, which is distributed in the...

    The Java. Native

    • integrating a Java application with legacy code written in languages such as C or C++ • incorporating a Java virtual machine implementation into an existing application written in languages such ...

    Mastering Concurrency in Go (PDF)

    an expanding user base by storm and has become one of the most popular languages (up there with stalwarts such as C, C++, and Java). Multithreading, memory caching, and APIs have allowed multiple ...

    Java2核心技术卷I+卷2:基础知识(第8版) 代码

    Calling a C Function from a Java Program 936 Numeric Parameters and Return Values 942 String Parameters 944 Accessing Fields 950 Encoding Signatures 954 Calling Java Methods 956 Accessing Array ...

    Learn Java for Android Development, 3rd Edition

    Learn Java for Android Development, 3rd Edition, is an update of a strong selling book that now includes a primer on Android app development (in Chapter 1 and Appendix C, which is distributed in the ...

    C in a Nutshell 2nd 原版pdf by Prinz & Crawford

    X3.159, which was ratified in late 1989 and is commonly called ANSI C or C89. The new features of the 2011 C standard are not yet fully supported by all compilers and standard library implementations....

    Addison.Wesley.The.Java.Programming.Language.4th.Edition.Aug.2005.chm

    giving overviews of those packages not covered in more detail in this book. <br>Appendix AApplication Evolutionlooks at some of the issues involved in dealing with the evolution of applications and...

    The Java Native Interface

    THIS book covers the Java™ Native Interface (JNI). It will be useful to you if you are interested in any of the following: ...how to handle features such as garbage collection and multithreading

    The Java Native Interface 原版pdf by Liang

    THIS book covers the Java™ Native Interface (JNI). It will be useful to you if you are interested in any of the following: ...how to handle features such as garbage collection and multithreading

    .Android.Apps.Performance.Optimization.pd

    How to optimize your Java code with the SDK, but also how to write and optimize native code using advanced features of the Android NDK such as using ARM single instruction multiple data (SIMD) ...

    JNI编程指南与规范.zip

    JNI程序员指南与规范.pdf + 06 JNI编程指南.pdf NDK开发汇总 ... THIS book covers the Java™ Native Interface (JNI)....you are interested in...how to handle features such as garbage collection and multithreading

    Programming Microsoft LINQ in Microsoft.NET Framework 4

    Paul Deitel and Harvey Deitel are the founders of Deitel & Associates, Inc., the internationally recognized programming languages authoring and corporate-training organization. Millions of people ...

    Core PYTHON Programming, 2nd Edition (pdf, Python 2.5)

    Improve the performance of your Python applications by writing extensions in C and other languages, or enhance I/O-bound applications by using multithreading Learn about Python’s database API and how...

    Core PYTHON Programming, 2nd Edition (epub 格式, Python 2.5)

    Improve the performance of your Python applications by writing extensions in C and other languages, or enhance I/O-bound applications by using multithreading Learn about Python’s database API and how...

    Android NDK Game Development Cookbook (安卓NDK游戏开发指南)

    You need to have basic knowledge of C or C++ including pointer manipulation, multithreading, and object-oriented programming concepts as well as some experience developing applications without using ...

    C++: Effective Modern C++ (C++ 11, C++ 14)

    C++: Effective Modern C++ (C++ 11, C++ 14) (guide,C Programming, HTML, Javascript, Programming,all,internet, Coding, CSS, Java, PHP Vol 1) By 作者: Paul Laurence ISBN-10 书号: 1547133244 ISBN-13 书号:...

Global site tag (gtag.js) - Google Analytics