Wednesday, December 29, 2010

DIFFERENCE OF KEYWORD 'PUBLIC','PRIVATE','PROTECTED'

[1] OReilly - Practical C++ Programming.chm By Steve Oualline Publisher : O'Reilly
Pub Date : December 2002 ISBN : 0-596-00419-2 Pages : 574 21.1 Derived Classes

DIFFERENCE OF KEYWORD 'PUBLIC','PRIVATE','PROTECTED'
----------------------------------------------------
So the three protection keywords are:
private
Access is limited to the class only.
protected
The class and any derived class that use the class as a base class can access the member.
public
Anyone can access the member.

Thursday, December 16, 2010

Monday, December 13, 2010

thread relevant function in Linux

CREATE THREADS
---------------------
int pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void*), void *restrict arg);

WAIT FOR THE FINISH OF THREAD
-------------------------------------
int pthread_join(pthread_t thread, void **value_ptr);

PROTECTION OF SHARED RESOURCE AMONG MULTIPLE THREADS
------------------------------------------------------
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
If the mutex is already locked, the calling thread shall block
until the mutex becomes available.

THREAD SYNCH BY BLOCKING ON A CONDITION VARIABLE
--------------------------------------------------
int pthread_cond_timedwait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex,
const struct timespec *restrict abstime);
int pthread_cond_wait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex);
Condition Variables: pthreads support condition variables, which
allow one thread to wait (sleep) for an event generated by any other thread.
This allows us to avoid the busy waiting problem.


THREAD SYNCH BY UNBLOCKING THREAD BLOCKED ON A CONDITION VARIABLE
------------------------------------------------------------------
int pthread_cond_broadcast(pthread_cond_t *cond); unblock all
int pthread_cond_signal(pthread_cond_t *cond); unblock at least one thread
The thread(s) that are unblocked shall contend for the mutex according to the
scheduling policy (if applicable), and as if each had called pthread_mutex_lock().



THREAD ATTRIBUTES SETTING
-----------------------------
int pthread_attr_destroy(pthread_attr_t *attr);
int pthread_attr_init(pthread_attr_t *attr);

SET THREAD SCHEDULING POLICY
---------------------------------
int pthread_attr_getschedparam(const pthread_attr_t *restrict attr,
struct sched_param *restrict param);
int pthread_attr_setschedparam(pthread_attr_t *restrict attr,
const struct sched_param *restrict param);

int pthread_attr_getscope(const pthread_attr_t *restrict attr,
int *restrict contentionscope);
int pthread_attr_setscope(pthread_attr_t *attr, int contentionscope);
contentionscope -- PTHREAD_SCOPE_SYSTEM,PTHREAD_SCOPE_PROCESS

int pthread_attr_getschedpolicy(const pthread_attr_t *restrict attr,
int *restrict policy);
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
policy -- SCHED_FIFO, SCHED_RR, SCHED_SPORADIC,and SCHED_OTHER

TERMINATE THREAD
----------------------
void pthread_exit(void *value_ptr);
If main() returns or any thread calls, exit()all threads are terminated



OTHER PASTRIES PTHREAD FUNCTIONS
-----------------------------------
int pthread_key_create(pthread_key_t *key, void (*destructor)(void*));
int pthread_once(pthread_once_t *once_control,
void (*init_routine)(void));
pthread_once_t once_control = PTHREAD_ONCE_INIT;
void *pthread_getspecific(pthread_key_t key);
int pthread_setspecific(pthread_key_t key, const void *value);


Be aware of a list of the Posix thread-safe functions

FW: 可重入函数

[1] http://blog.chinaunix.net/u2/68846/showart_689446.html


把一个不可重入函数变成可重入的唯一方法是用可重入规则来重写他。
其实很简单,只要遵守了几条很容易理解的规则,那么写出来的函数就是可重入的。

第一,不要使用全局变量。因为别的代码很可能覆盖这些变量值。
第二,在和硬件发生交互的时候,切记执行类似disinterrupt()之类的操作,就是关闭硬件中断。完成交互记得打开中断,在有些系列上,这叫做“进入/退出核心”或者用OS_ENTER_KERNAL/OS_EXIT_KERNAL来描述。
第三,不能调用任何不可重入的函数。
第四,谨慎使用堆栈。最好先在使用前先OS_ENTER_KERNAL。

还有一些规则,都是很好理解的,总之,时刻记住一句话:保证中断是安全的!

Sunday, December 12, 2010

makefile of ThreadTest Program

[q.yang@localhost Sample_011_pThread_test]$ cat makefile
DESTDIR = ./debug
PROGRAM = $(DESTDIR)/ThreadTest
SRCDIR = ./
C = g++
OBJS := $(addprefix $(DESTDIR)/,task1.o)
DEBUG = -g
CFLAGS = -Wall $(DEBUG)

$(PROGRAM): $(OBJS)
$(C) -o $(PROGRAM) $(OBJS)

-lpthread



$(DESTDIR)/%.o:$(SRCDIR)/%.cpp
$(C) -c $(CFLAGS) $< -o $@ $(INCL)

clean :
rm -f $(OBJS)
rm -f $(PROGRAM)

Thursday, December 9, 2010

Thread Vs Process

[1] http://www.cs.rpi.edu/~hollingd/netprog2001/notes/threads/threads.pdf
[2] http://blog.csdn.net/lanmoshui963/archive/2008/03/13/2176376.aspx
[3] sample thread code. http://paste.ubuntu.org.cn/809
[4] http://blog.chinaunix.net/u2/68846/showart_1077115.html
[5] http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html MultiThread Debugging
[6] http://www.linuxselfhelp.com/HOWTO/C++Programming-HOWTO-18.html
[7] http://www.codeproject.com/KB/threads/threadobject.aspx Sample Codes CThread
[8] http://www.ibm.com/developerworks/cn/linux/thread/posix_threadapi/part2/ ThreadProgGuide
[9] http://blog.chinaunix.net/u/22935/showart_310711.html CN BLOG of thread prog


多线程可以把程序中处理用户输入输出的部分与其它部分分开。
线程的缺点 线程也有不足之处。编写多线程程序需要更全面更深入的思考。在一个多线程程序里,因时间分配上的细微偏差或者因共享了不该共享的变量而造成不良影响的可能性是很大的。调试一个多线程程序也比调试一个单线程程序困难得多。
进程的所有信息对该进程的所有线程都是共享的,包括可执行的程序文本,程序的全局内存和堆内存、栈以及文件描述符。[9]

The static function will then typecast the void * and use it to call a non static member function. [6]

从逻辑角度来看,多线程的意义在于一个应用程序中,有多个执行部分可以同时执行。但操作系统并没有将多个线程看做多个独立的应用,来实现进程的调度和管理以及资源分配。这就是进程和线程的重要区别。

线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位.线程自己基本上不拥有系统资源,只拥有一点在运行中必不可少的资源(如程序计数器,一组寄存器和栈),但是它可与同属一个进程的其他的线程共享进程所拥有的全部资源。

一个线程可以创建和撤销另一个线程,同一个进程中的多个线程之间可以并发执行。

线程有自己的堆栈和局部变量,但线程之间没有单独的地址空间,一个线程死掉就等于整个进程死掉,所以多进程的程序要比多线程的程序健壮,但在进程切换时,耗费资源较大,效率要差一些。但对于一些要求同时进行并且又要共享某些变量的并发操作,只能用线程,不能用进程。

对开发人员来说,线程之间的数据共享比进程之间的容易得多。在 unix 上,进程间数据共享是通过管道、SOCKET、共享内存、信号灯等机制实现的,在 windows 上也有一套类似的机制。而线程间数据共享只需要共享全局变量即可。多进程无疑比多线程程序更健壮一些,但是代价也是比较大的,特别在进程数或者线程数较多的情况下。

进程在执行过程中有内存单元的初始入口点,并且进程存活过程中始终拥有独立的内存地址空间;

●进程的生存期状态包括创建、就绪、运行、阻塞和死亡等类型;

●从应用程序进程在执行过程中向CPU发出的运行指令形式不同,可以将进程的状态分为用户态和核心态。处于用户态下的进程执行的是应用程序指令、处于核心态下的应用程序进程执行的是操作系统指令。

在Unix操作系统启动过程中,系统自动创建swapper、init等系统进程,用于管理内存资源以及对用户进程进行调度等。在Unix环境下无论是由操作系统创建的进程还要由应用程序执行创建的进程,均拥有唯一的进程标识(PID)。[4]


Creation of a new process using fork is expensive (time & memory).
A thread (sometimes called a lightweight process) does not require lots of memory or startup time.


Each process can include many threads.
All threads of a process share:
– memory (program code and global data)
– open file/socket descriptors
– signal handlers and signal dispositions
– working environment (current directory, user ID,
etc.)



Each thread has it’s own:
– Thread ID (integer)
– Stack, Registers, Program Counter
– errno (if not - errno would be useless!)
Threads within the same process can
communicate using shared memory.
Must be done carefully!


We will focus on Posix Threads - most widely
supported threads programming API.
You need to link with “-lpthread”
On many systems this also forces the
compiler to link in re-entrant libraries
(instead of plain vanilla C libraries).



pthread_create(
pthread_t *tid,
const pthread_attr_t *attr,
void *(*func)(void *),
void *arg);
func is the function to be called.
When func() returns the thread is terminated.



• The return value is 0 for OK.
positive error number on error.
• Does not set errno !!!
• Thread ID is returned in tid



Thread Arguments (cont.)
----------------------------------
Complex parameters can be passed by creating
a structure and passing the address of the
structure.
The structure can't be a local variable (of the
function calling pthread_create)!!
- threads have different stacks!



Joinable Thread
------------------------------
Joinable: on thread termination the thread ID
and exit status are saved by the OS.
One thread can "join" another by calling
pthread_join- which waits (blocks)
until a specified thread exits.
int pthread_join( pthread_t tid,
void **status);


Shared Global Variables
---------------------------------
int counter=0;
void *pancake(void *arg) {
counter++;
printf("Thread %u is number %d\n",
pthread_self(),counter);
}
main() {
int i; pthread_t tid;
for (i=0;i<10;i++)
pthread_create(&tid,NULL,pancake,NULL);
}


Locking and Unlocking
----------------------------
• To lock use:
pthread_mutex_lock(pthread_mutex_t &);
• To unlock use:
pthread_mutex_unlock(pthread_mutex_t &);
• Both functions are blocking!


Condition Variables (cont.)
------------------------------
A condition variable is always used with mutex.
pthread_cond_wait(pthread_cond_t *cptr,
pthread_mutex_t *mptr);
pthread_cond_signal(pthread_cond_t
*cptr);
don’t let the word signal confuse you -
this has nothing to do with Unix signals










摘自资料(linux 与Windows不同)

线程间无需特别的手段进行通信,因为线程间可以共享数据结构,也就是一个全局变量可以被两个线程同时使用。不过要注意的是线程间需要做好同步,一般用 mutex。可以参考一些比较新的UNIX/Linux编程的书,都会提到Posix线程编程,比如《UNIX环境高级编程(第二版)》、《UNIX系统编程》等等。 linux的消息属于IPC,也就是进程间通信,线程用不上。

linux用pthread_kill对线程发信号。 另:windows下不是用post..(你是说PostMessage吗?)进行线程通信的吧?

windows用PostThreadMessage进行线程间通信,但实际上极少用这种方法。还是利用同步多一些 LINUX下的同步和Windows原理都是一样的。不过Linux下的singal中断也很好用。

用好信号量,共享资源就可以了。

使用多线程的理由之一是和进程相比,它是一种非常"节俭"的多任务操作方式。我们知道,在Linux系统下,启动一个新的进程必须分配给它独立的地址空间,建立众多的数据表来维护它的代码段、堆栈段和数据段,这是一种"昂贵"的多任务工作方式。而运行于一个进程中的多个线程,它们彼此之间使用相同的地址空间,共享大部分数据,启动一个线程所花费的空间远远小于启动一个进程所花费的空间,而且,线程间彼此切换所需的时间也远远小于进程间切换所需要的时间。

  使用多线程的理由之二是线程间方便的通信机制。对不同进程来说,它们具有独立的数据空间,要进行数据的传递只能通过通信的方式进行,这种方式不仅费时,而且很不方便。线程则不然,由于同一进程下的线程之间共享数据空间,所以一个线程的数据可以直接为其它线程所用,这不仅快捷,而且方便。当然,数据的共享也带来其他一些问题,有的变量不能同时被两个线程所修改,有的子程序中声明为static的数据更有可能给多线程程序带来灾难性的打击,这些正是编写多线程程序时最需要注意的地方。
1、简单的多线程程序

首先在主函数中,我们使用到了两个函数,pthread_create和pthread_join,并声明了一个pthread_t型的变量。
pthread_t在头文件pthread.h中已经声明,是线程的标示符

函数pthread_create用来创建一个线程,函数原型:

extern int pthread_create __P ((pthread_t *__thread, __const pthread_attr_t *__attr,void *(*__start_routine) (void *), void *__arg));

  第一个参数为指向线程标识符的指针,第二个参数用来设置线程属性,第三个参数是线程运行函数的起始地址,最后一个参数是运行函数的参数。若我们的函数thread不需要参数,所以最后一个参数设为空指针。第二个参数我们也设为空指针,这样将生成默认属性的线程。对线程属性的设定和修改我们将在下一节阐述。当创建线程成功时,函数返回0,若不为0则说明创建线程失败,常见的错误返回代码为EAGAIN和EINVAL。前者表示系统限制创建新的线程,例如线程数目过多了;后者表示第二个参数代表的线程属性值非法。创建线程成功后,新创建的线程则运行参数三和参数四确定的函数,原来的线程则继续运行下一行代码。
函数pthread_join用来等待一个线程的结束。函数原型为:

  extern int pthread_join __P ((pthread_t __th, void **__thread_return));

  第一个参数为被等待的线程标识符,第二个参数为一个用户定义的指针,它可以用来存储被等待线程的返回值。这个函数是一个线程阻塞的函数,调用它的函数将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源被收回。一个线程的结束有两种途径,一种是象我们上面的例子一样,函数结束了,调用它的线程也就结束了;另一种方式是通过函数pthread_exit来实现。它的函数原型为:

  extern void pthread_exit __P ((void *__retval)) __attribute__ ((__noreturn__));

  唯一的参数是函数的返回代码,只要pthread_join中的第二个参数thread_return不是NULL,这个值将被传递给 thread_return。最后要说明的是,一个线程不能被多个线程等待,否则第一个接收到信号的线程成功返回,其余调用pthread_join的线程则返回错误代码ESRCH。

2、修改线程的属性
设置线程绑定状态的函数为pthread_attr_setscope,它有两个参数,第一个是指向属性结构的指针,第二个是绑定类型,它有两个取值:PTHREAD_SCOPE_SYSTEM(绑定的)和PTHREAD_SCOPE_PROCESS(非绑定的)。下面的代码即创建了一个绑定的线程。

#include
pthread_attr_t attr;
pthread_t tid;

/*初始化属性值,均设为默认值*/
pthread_attr_init(&attr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

pthread_create(&tid, &attr, (void *) my_function, NULL);

3、线程的数据处理

和进程相比,线程的最大优点之一是数据的共享性,各个进程共享父进程处沿袭的数据段,可以方便的获得、修改数据。但这也给多线程编程带来了许多问题。我们必须当心有多个不同的进程访问相同的变量。许多函数是不可重入的,即同时不能运行一个函数的多个拷贝(除非使用不同的数据段)。在函数中声明的静态变量常常带来问题,函数的返回值也会有问题。因为如果返回的是函数内部静态声明的空间的地址,则在一个线程调用该函数得到地址后使用该地址指向的数据时,别的线程可能调用此函数并修改了这一段数据。在进程中共享的变量必须用关键字volatile来定义,这是为了防止编译器在优化时(如gcc中使用 -OX参数)改变它们的使用方式。为了保护变量,我们必须使用信号量、互斥等方法来保证我们对变量的正确使用。

4、互斥锁

互斥锁用来保证一段时间内只有一个线程在执行一段代码。必要性显而易见:假设各个线程向同一个文件顺序写入数据,最后得到的结果一定是灾难性的

Wednesday, December 8, 2010

Check GDB version in Fefora

[q.yang@localhost ProjProgrammingTest]$ gdb
GNU gdb Fedora (6.8-24.fc9)
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu".
(gdb) help
List of classes of commands:

aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands

Type "help" followed by a class name for a list of commands in that class.
Type "help all" for the list of all commands.
Type "help" followed by command name for full documentation.
Type "apropos word" to search for commands related to "word".
Command name abbreviations are allowed if unambiguous.