2007年3月21日星期三

POXIS Thread

创建线程
struct sockets {
int local;
FILE *in, *out;
};

{
struct sockets s;
s.local = 1;
s.in = stdin;
s.out = stdout;
 pthread_create(&p, NULL, roll_dice, (void *) &s);
}
pthread_create() 将接受一个 void * 类型的参数,这个参数会被传到线程开始执行的那个函数中去。这样允许您创建一个任意复杂的数据结构,并将它作为一个指针传送给需要在这个数据结构上进行操作的线程。

线程函数对参数的处理
/* read dice on standard input, write results on standard output */
void *roll_dice(void *v) {
struct sockets *s = v;
char inbuf[512];

/* think globally, program defensively */
if (!s || !s->out || !s->in)
return NULL;

fprintf(s->out, "enter die rolls, or q to quit\n");
...
}
http://www.ibm.com/developerworks/cn/linux/l-pthred/index.html

pthreads 的基本用法


pthread_exit 线程终止
#include
void pthread_exit(void *value_ptr);
pthread_exit()函数将终止调用线程, 并使value_ptr值对任何与结束的线程成功结合(的线程)可用. 任何取消清除的已经压入并还没有压出的处理将以与压入相反的顺序压出并执行. 在所有取消清除的操作执行完毕后, 如果线程还有任何线程特定的数据, 相应的销毁函数将以不确定的顺序被调用. 线程终止并不释放任何应用程序可见进程资源, 包括, 但不仅仅是, 互斥和文件描述符, 它也不会执行任何进程级的清除操作, 包括, 但不限于, 调用任何可能退出的atexit()函数.

调用pthread_exit()的一个应用是当一个除了在第一次在main()调用的线程从用于创建它的起始程序中返回. 函数的返回值应用于线程的退出状态.

pthread_exit()如果从取消清除操作或作为隐式或显式的调用结果的销毁函数中调用, 其行为是不明确的.

在一个线程终止后, 线程内部的本地(自动)变量获取的结果是不明确的. 这样, 参考退出线程的本地变量不能作为pthread_exit() value_ptr参数值使用.

进程应该在最后一个线程终止后以带有0退出状态退出. 这种行为就像在线程终止时使用带有zero参数值的exit().

pthread_exit()--Terminate Calling Thread


Syntax:
 #include 
void pthread_exit(void *status);
Threadsafe: Yes

Signal Safe: No

The pthread_exit() function terminates the calling thread, making its exit status available to any waiting threads. Normally, a thread terminates by returning from the start routine that was specified in the pthread_create() call which started it. An implicit call to pthread_exit() occurs when any thread returns from its start routine. (With the exception of the initial thread, at which time an implicit call to exit() occurs). The pthread_exit() function provides an interface similar to exit() but on a per-thread basis.

Note that in the OS/400 implementation of threads, the initial thread is special. Termination of the initial thread by pthread_exit() or any thread termination mechanism terminates the entire process.

The following activities occur in this order when a thread terminates by a return from its start routine or pthread_exit() or thread cancellation:

  1. Any cancellation cleanup handlers that have been pushed and not popped will be executed in reverse order with cancellation disabled.
  2. Data destructors are called for any thread specific data entries that have a non NULL value for both the value and the destructor.
  3. The thread terminates.
  4. Thread termination may possibly cause the system to run OS/400 cancel handlers (registered with the #pragma cancel_handler directive), or C++ destructors for automatic objects.
  5. If thread termination is occurring in the initial thread, it will cause the system to terminate all other threads, then run C++ static object destructors, activation group cleanup routines and atexit() functions.
  6. Any mutexes that are held by a thread that terminates, become `abandoned' and are no longer valid. Subsequent calls by other threads that attempt to acquire the abandoned mutex though pthread_mutex_lock() will deadlock. Subsequent calls by other threads that attempt to acquire the abandoned mutex through pthread_mutex_trylock() will return EBUSY.
  7. No release of any application visible process resources occur. This includes but is not limited to mutexes, file descriptors, or any process level cleanup actions.

Do not call pthread_exit() from a cancellation cleanup handler or destructor function that was called as a result of either an implicit or explicit call to pthread_exit(). If pthread_exit() is called from a cancellation cleanup handler, the new invocation of pthread_exit() will continue cancellation cleanup processing using the next cancellation cleanup handler that was pushed. If pthread_exit() is called from a data destructor, the new invocation of pthread_exit() will skip all subsequent calls to any data destructors (regardless of the number of destructor iterations that have completed), and terminate the thread.

Cleanup handlers and data destructors are not called when the application calls exit() or abort() or otherwise terminates the process. Cleanup handlers and data destructors are not called when a thread terminates by any proprietary OS/400 mechanism other than the Pthread interfaces.

The meaning of the status parameter is determined by the application except for the following conditions:

  1. When the thread has been canceled using pthread_cancel(), the exit status of PTHREAD_CANCELED will be made available.
  2. When the thread has been terminated as a result of an unhandled OS/400 exception, operator intervention or other proprietary OS/400 mechanism, the exit status of PTHREAD_EXCEPTION_NP will be made available.

No address error checking is done on the status parameter. Do not call pthread_exit() with, or return the address of, a variable in a threads automatic storage. This storage will be unavailable after the thread terminates.

Note: If pthread_exit() is called by application code after step 3 in the above list, pthread_exit() will fail with the CPF1F81 exception. This indicates that the thread is already considered terminated by the system, and pthread_exit() cannot continue. If your code does not handle this exception, it will appear as if the call to pthread_exit() was successful.


Authorities and Locks

None.


Parameters

status
(Input) exit status of the thread

Return Value

pthread_exit() does not return.


Error Conditions

None.


Related Information

没有评论: