Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Тестовые примеры / ch13 / pkg / thr_errno
.C#include "pkg.h"
Xpackage pkgObj; /* a package object */
/* function executed by each thread */
void* func1( void* argp )
{
int *rcp = new int(1);
/* open a thread's outstream */
ofstream ofs ((char*)argp);
if (!ofs) thr_exit((void**)&rcp);
/* initialize a thread-specific data */
pkgObj.new_thread( 0, ofs );
/* do work with package functions here */
pkgObj.chgErrno( 100 ); /* change a thread's errno */
/* write some data to a thread's outstream */
pkgObj.os() << (char*)argp << " ["
<< (int)thr_self() << "] finishes\n";
/* thread termimnates, set exit code */
*rcp = pkgObj.errno();
thr_exit(rcp);
return 0;
}
/* main thread's function */
int main(int argc, char** argv)
{
thread_t tid;
int *rc;
/* create a thread for each command line argument */
while (--argc > 0)
if (thr_create(0,0,func1,(void*)argv[argc],0,&tid))
perror("thr_create");
/* wait for all threads to terminate */
while (!thr_join(0,&tid,(void**)&rc))
{
cerr << "thread: " << (int)tid << " exists. rc=" << *rc << endl;
delete rc;
}
/* terminate the main thread */
thr_exit(0);
return 0;
}