Solved

How to create thread in UNUX

Posted on 2000-02-29
5
171 Views
Last Modified: 2010-04-21
I would like to know how to create threads in UNIX systems using C/C++ with out nonstandard libraries and to obtain some example programs. How to run, stop and kill threads from parent process?
0
Comment
Question by:MaxZ
5 Comments
 
LVL 1

Accepted Solution

by:
hansendc earned 100 total points
ID: 2570088
Here is a fun little program that should get you started.  It uses the POSIX thread library, which is standard.  Do a "man pthread_create" on a UNIX system to get some good info.


#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

int count;

void increment( int ntimes )
{
        for ( int i = 0; i < ntimes; i++ ) {
                int c;

                c = count;
                c = c + 1;

                count = c;
        }
}

int main( int argc, char ** argv )
{
        int n = 10000000;
        pthread_t t1, t2;
        pthread_attr_t attr;

        pthread_attr_init( &attr );
        pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

        printf("Start Test. Final count should be %d\n", 2 * n );

        // Create threads
        pthread_create( &t1, &attr, (void * (*)(void *)) increment,
                        (void *) n);

        pthread_create( &t2, &attr, (void * (*)(void *)) increment,
                        (void *) n);

        // Wait until threads are done
        pthread_join( t1, NULL );
        pthread_join( t2, NULL );

        if ( count != 2 * n ) {
                printf("\n****** Error. Final count is %d\n", count );
                printf("****** It should be %d\n", 2 * n );
        }
        else {
                printf("\n>>>>>> O.K. Final count is %d\n", count );
        }
}
0
 
LVL 2

Expert Comment

by:curri
ID: 2585572
There is NO support for threads in standard C or standard C++. You can find several different 'standards' for threads (and probably the best is the posix one that is mentioned above)
0
 
LVL 1

Expert Comment

by:hansendc
ID: 2585630
While correct about not having it be a part of the language in C++ or C, neither is printf.  
MaxZ, has your question been answered?
0
 

Expert Comment

by:ramaswamys
ID: 2586672
You can find answers to your questions and a whole lot of other useful links at the  'The LinuxThreads Library' site (http://pauillac.inria.fr/~xleroy/linuxthreads/ ).

Ramaswamy
0
 

Author Comment

by:MaxZ
ID: 2586706
Thank you for your answer. It was exactly what I needed.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now