멀티스레딩 실전 방법

Bang!·2022년 1월 19일
0

OS(오퍼레이션)

목록 보기
5/7

Pthreads

• One of three primary thread libraries is POSIX Pthreads

#include <stdio.h>
#include <stdlib.h>
int sum; / this data is shared by the thread(s) /
void runner(void param); / threads call this function /

int main(int argc, char argv[])
{
pthread t tid; /
the thread identifier /
pthread attr t attr; /
set of thread attributes */

/* set the default attributes of the thread */
pthread attr init(&attr);


/* create the thread */
pthread create(&tid, &attr, runner, argv[1]);

/* wait for the thread to exit */
pthread join(tid, NULL);
printf("sum = %d∖n", sum);

}
/ The thread will execute in this function /

void runner(void param)
{
int i, upper = atoi(param);
sum = 0;
for (i = 1; i <= upper; i++)
sum += i;

pthread exit(0);

}

  • All Pthreads programs must include the pthread.h header file.
  • The example demonstrates the basic Pthreads API for constructing a multithreaded program
    that calculates the summation of a non-negative integer in a separate thread.
1. In a Pthreads program, separate threads begin execution in a specified function. 
• In the example, 'runner()' function 
2. When we execute the program, a single thread of control begins in main
3. main() creates a second thread that begins control in the runner() function.
	a. Both threads share the global data sum.
	b. 'pthread t tid' declares the identifier for the thread we will create
	c. 'pthread attr t attr' declaration represents the attributes for the thread
	d. 'pthread attr init(&attr)' function call is ued to set attributes is there
		i. Because… we did not explicitly set any attributes, we use the default attributes provided.
	e. separate thread is created with the pthread_create() function call.
		i. pass the name of the function where the new thread will begin (in 'runner()" function)
		ii. pass the integer parameter that was provided on the command line, argv[1]
4. Now the program has two threads(parent thread in main(), child thread performing in the runner())
5. program follows the thread create/join strategy, 
	a. after creating the summation thread, the parent thread will wait for it to terminate by calling the pthread join() function. 
	b. The summation thread will terminate when it calls the function pthread exit(). 
	c. When summation thread has returned, the parent thread will output the value of the shared data sum.
  • This exmaple creates only single thread!!!
  • simple method for waiting on several threads using the pthread join() function is to enclose the operation within a simple for loop.

Pthreads thread cancellation

  • In Pthreads, thread cancellation is initiated by using pthread_cancel() function
    Ex:
    pthread t tid;
    / create the thread /
    pthread create(&tid, 0, worker, NULL);
    ...
    / cancel the thread /
    pthread cancel(tid);
    / wait for the thread to terminate /
    pthread join(tid, NULL)
  • Invoking pthread cancel()indicates only a request to cancel the target thread

  • actual cancellation depends on how the target thread is set up to handle the request.

  • When target thread is canceled, the call to pthread join() in the canceling thread returns.

  • Pthreads supports three cancellation modes….

  • Pthreads allows threads to disable or enable cancellation.
    • a thread cannot be canceled if cancellation is disabled.

  • However, cancellation requests remain pending, so the thread can later enable cancellation and respond to the request.

  • The default cancellation
    • type is deferred cancellation.
    • cancellation occurs only when a thread reaches a cancellation point.

  • One technique for establishing a cancellation point is to invoke the pthread testcancel() function
    • If a cancellation request is found to be pending….
    ○ the call to pthread testcancel() will not return
    ○ Thread will terminate;
    • If not pending….
    ○ the call to the function will return, and the thread will continue to run.
  • Pthreads allows a function known as a cleanup handler to be invoked if a thread is canceled.
    • Allows any resources a thread may have acquired to be released before the thread is terminated.
    Ex:
    while (1) {
    / do some work for awhile /
    ...
    / check if there is a cancellation request /
    pthread testcancel();
    }

In Java…..

Thread worker;
...
/ set the interruption status of the thread /
worker.interrupt()

  • A thread can check its interruption status by invoking isInterrupted() method,
    • returns a boolean value of a thread’s interruption status
    while (!Thread.currentThread().isInterrupted()) {
    ...
    }
profile
pro한 프로그래머가 되자!

0개의 댓글