HomeLinuxPthread_Function() in C Language

Pthread_Function() in C Language


The POSIX commonplace provides us the chance to develop multithreaded packages within the C language. This programming method consists of utilizing the “pthread” library to open a number of subprocess threads that run in parallel with the principle course of.

The implementation of threads brings nice advantages to the ultimate program because it quickens the execution occasions by operating a number of duties concurrently, could be higher structured, and makes a greater use of system sources.

A thread is created with the pthread_ create() perform. As soon as this perform is named, the thread begins operating in parallel with the principle course of. When this perform is named, it returns the thread ID within the output argument thread. This identifier is used to seek advice from the created thread once you use the thread administration capabilities like pthread_join() or pthread_cancel().

On this Linux Trace article, you’ll discover ways to use the pthread_cancel() perform, one of many C thread administration capabilities that allows you to cancel a thread that’s already operating.

Syntax of the Pthread_Cancel() Perform in C Language

int pthread_cancel(pthread_t thread);

Description of the Pthread_Cancel() Perform in C Language

The pthread_cancel() perform sends a request to the system to cancel the thread that’s specified by the identifier in its enter argument thread.

The decision to the pthread_cancel() perform doesn’t indicate quick termination of the thread, however a request to take action. Whether or not a thread is terminated after calling this perform is determined by the attributes which might be assigned to it when it’s created. If a thread is created with its default attributes, it may be cancelled.

The cancel attributes of a thread could be retrieved with the pthread_setcancelstate() perform and could be set with pthread_setcanceltype().

If the pthread_cancel() perform is profitable, it returns 0 because the outcome. If it fails, it returns a nonzero worth.

The pthread_cancel() perform from the pthread library is outlined within the “thread.h” header. To make use of this and the opposite thread administration capabilities, you could embody this header in your program as follows:

The packages that use the thread administration capabilities have to be compiled by calling the thread library from the Linux command line. In any other case, the errors will happen.

The next is a particular part that explains the right way to appropriately compile the packages that use the capabilities from the thread library.

The way to Cancel a Operating Thread Utilizing the Pthread_Cancel() Perform in C Language

On this instance, we create a thread with the pthread_create() perform after which cancel it in the midst of execution with the pthread_cancel() perform.

To do that, we create the “thread_1” thread which executes the th_function() perform which consists of a for loop with 10 cycles, every of which prints the “Seconds from init thread and the variety of seconds elapsed” sentence within the command console. After that, a delay of 1 second is inserted to repeat the cycle once more. So, the execution time of the thread is about 10 seconds.

As soon as thread_1 is created and operating, we introduce a 5-second delay through the principle() perform and terminate the subprocess after half the execution time. To terminate the thread, we name the pthread_cancel() perform and cross the thread_1 identifier that’s returned by the pthread_create() perform as enter argument when the thread is created. Now, allow us to have a look at the code for this instance:

#embody <stdio.h>

#embody <stdlib.h>

#embody <unistd.h>

#embody <pthread.h>

void * th_function (void * n);

void  foremost()
{
pthread_t thread_1;
pthread_create(&thread_1, NULL, &th_function, NULL);
sleep(5);
pthread_cancel(thread_1);
sleep(5);
}

/******************************************************/
void * th_function (void* n )
{
for (int sec =0; sec!=10;sec++)
    {
    printf (“Seconds from init thread %in, sec);
    sleep(1);
    }
}

The next picture exhibits the compilation and execution of the code. As you possibly can see, the thread is terminated with the pthread_cancel() perform in the midst of the execution or 5 seconds after the beginning:

On this case, the thread accepts the cancellation as a result of its attributes are set to default at creation by specifying a NULL pointer within the “attr” enter arguments of the pthread_create() perform.

Errors within the Use of the Pthread_Cancel() Perform: What Are They and The way to Determine Them

The way in which to find out if an error happens when calling this perform is to make use of an “if” situation the place the enter situation is a non-zero return outcome. The next is a code snippet utilizing this methodology for error detection:

if (pthread_cancel (thread) !=0){

printf(“An error occurred attempting to cancel the desired thread”);

}

The one error that this perform can generate is the one that’s outlined as ISRCH within the “errno.h” header. This error is generated when a non-existent thread is referenced within the enter argument of the pthread_cancel() perform.

Compilation Errors in Applications with Threads

When compiling the GCC packages that use threads, the compilation might fail if it isn’t performed appropriately from the command line.

The most typical error message that’s issued by the compiler states that one of many thread capabilities that we seek advice from within the code isn’t outlined.

These errors usually end in losing useful time by checking the headers that we inserted within the code, their integrity, and the directories which might be related to the compiler since all the things signifies that the issue is there.

Though the capabilities that trigger the error are outlined within the “pthread.h” header and are included within the code, the compiler doesn’t acknowledge these capabilities until the pthread library is named from the command line throughout compilation.

You’ll be able to see in inexperienced the proper technique to name the pthread library from the command console throughout compilation of packages with threads within the following:

~$ gcc pthread path/filename.c o out_name

As we will see within the following determine, the error disappears when the pthread library is named throughout compilation:

Conclusion

On this Linux Trace article, we confirmed you the right way to use the pthread_cancel() perform to ask the system to terminate a operating thread.

We checked out a theoretical description of this perform and confirmed you its syntax, the enter and output arguments, and the kind of knowledge that every of them accepts. After that, we applied what we realized via a sensible instance with code snippets and pictures the place we now have seen how the pthread_cancel() perform works within the C language.

We additionally added a particular part the place we confirmed you the right way to appropriately compile the packages that comprise the threads and capabilities from the pthread library.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments